tesla授權(quán)過(guò)程

大致流程流程如下

  • 通過(guò)webview加載獲取返回的授權(quán)碼(code).
  • 通過(guò)code交換獲取到access_token refresh_token expires_in等信息.
  • 進(jìn)而實(shí)現(xiàn)其他操作.

com.google.api-client:google-api-client:1.34.1
適用于 Java 的 Google OAuth 客戶端庫(kù)可與網(wǎng)絡(luò)上的任何 OAuth 服務(wù)配合使用,而不僅僅是與 Google API 配合使用

1. 項(xiàng)目配置
 repositories {
      mavenCentral()
  }
  dependencies {
      compile 'com.google.api-client:google-api-client:1.32.1'
  }


2. 配置
private static final String CLIENT_ID = "ownerapi";
private static final String CLIENT_SECRET = "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3";

AuthorizationCodeFlow.Builder builder = new AuthorizationCodeFlow.Builder(
  BearerToken.authorizationHeaderAccessMethod(),
  new NetHttpTransport(),
  new GsonFactory(),
  new GenericUrl(tokenUrl),
  new ClientParametersAuthentication(CLIENT_ID, CLIENT_SECRET),
  CLIENT_ID,
  authUrl
).setScopes(Arrays.asList("openid", "email", "offline_access")).enablePKCE();

flow = builder.build();

3. 獲取授權(quán)code的url
url=flow.newAuthorizationUrl().setRedirectUri(redirectUrl).setState(getRandomString(6)).build()

3.1 webview會(huì)進(jìn)行cookie管理,且oauth中需要cookie才可執(zhí)行。此處需要在webview開(kāi)始前去掉所有緩存的cookies
cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(null); // 或根據(jù)url,自己處理.
cookieManager.setAcceptCookie(true);

4. 通過(guò)第三步返回的code執(zhí)行交換
TokenResponse response = flow.newTokenRequest(code).setGrantType("authorization_code").setRedirectUri(redirectUrl).execute();

5. 搞定! God bless U

Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容