8

我的Android應用程序當前使用GoogleAuthUtil登錄用戶並獲取傳遞到後端的access_token(下面的代碼片斷顯示創建GoogleApiClient並使用GoogleAuthUtil獲取令牌)。Google登錄使用新的GoogleSignInOptions獲取訪問令牌

mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(Plus.API) 
     .addScope(new Scope("profile")) 
     .build(); 
... 
... 

String accessToken = GoogleAuthUtil.getToken(GoogleLoginActivity.this, 
          Plus.AccountApi.getAccountName(mGoogleApiClient), 
          "oauth2:profile email"); 

然後我發送給後端

現在我想移動到新的谷歌登入 - https://developers.google.com/identity/sign-in/android/sign-in

,因此改變了GoogleApiClient創建類似,

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
     .requestEmail() 
     .requestIdToken("<web client id>") 
     .build(); 
mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .enableAutoManage(this, this) 
     .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
     .build(); 

然後做登錄使用,

startActivityForResult(Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient), RC_GET_TOKEN); 

和活動結果的使用(類似於上面的鏈接的例子),

OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient); 
if (opr.isDone()) { 
    // If the user's cached credentials are valid, the OptionalPendingResult will be "done" 
    // and the GoogleSignInResult will be available instantly. 
    Log.d(TAG, "Got cached sign-in"); 
    handleSignInResult(opr.get()); 
} else { 
    // If the user has not previously signed in on this device or the sign-in has expired, 
    // this asynchronous branch will attempt to sign in the user silently. Cross-device 
    // single sign-on will occur in this branch. 
    showProgress(); 
    opr.setResultCallback(new ResultCallback<GoogleSignInResult>() { 
     @Override 
     public void onResult(GoogleSignInResult googleSignInResult) { 
      hideProgress(); 
      handleSignInResult(googleSignInResult); 
     } 
    }); 
} 

,但現在看來,在handleSingInResult(GoogleSignInResult result)我只能得到一個id token回來 result.getSignInAccount().getIdToken();

有誰知道是否有可能從此獲取訪問令牌(如以前一樣),如果是這樣的話?任何幫助讚賞。

+0

我在這裏遇到同樣的問題,你是否設法解決了這個問題? –

+0

不是。必須在後端實現id令牌處理,然後才能開始使用它。 – Bootstrapper

+0

我最終使用Android的帳戶選擇器獲取帳戶名稱,並使用GoogleAuthUtil.getToken(Context,accoutName,scope)來檢索令牌本身,它像魅力一樣工作,然後比第一個Approuch簡單得多。 –

回答

4

在您簽字後就能拿到令牌:

final String token = GoogleAuthUtil.getToken(mAppContext, mAccountName, AUTH_TOKEN_TYPE); 

不要忘記做的的AsyncTask。更多的細節來看看here

編輯:

需要注意的是,儘管該方法的名稱:

GoogleAuthUtil.getToken() 

它不會給你一個OAuth令牌,它,而會返回一個「短根據documentation的授權代碼。

通過調用 GoogleAuthUtil.getToken()獲得授權碼後應該怎麼做?

您應該通過HTTPS將授權碼傳輸到您的後端服務器。只有從你的服務器你應該嘗試接收訪問和/或刷新令牌,而不是在你的應用程序。

+0

這不起作用。 – Vyacheslav

+0

仍然在這裏工作 – abedfar

+5

@aberdfar,這個方法已被棄用 – Vyacheslav

2

所以我遇到了同樣的問題。所以令牌來通過

GoogleSignInAccount acct = result.getSignInAccount(); 
Log.d(TAG, "handleSignInResult2: "+acct.getIdToken()); 

要訪問過此令牌,你有

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestEmail().requestProfile().requestId().requestIdToken(getString(R.string.server_client_ID)) 
        .build(); 

R.string.server_client_ID也要求它是從項目的client ID,你在做他們現在已經改變了它您的Google開發者控制檯。

我希望這可以幫助你。

這裏也是我遵循的文檔。 https://developers.google.com/identity/sign-in/android/backend-auth

+3

正如我在我的問題中提到的那樣,以這種方式收到的令牌是'id_token'而不是'access_token'。因此,現在看起來我們仍然需要像以前一樣在AsyncTask中使用'GoogleAuthUtil.getToken()'來獲取'access_token'(我希望有另一種方式,但是無論如何...) – Bootstrapper

+0

這是正確的。需要將id_token交換爲access_token。 –

相關問題