我的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();
有誰知道是否有可能從此獲取訪問令牌(如以前一樣),如果是這樣的話?任何幫助讚賞。
我在這裏遇到同樣的問題,你是否設法解決了這個問題? –
不是。必須在後端實現id令牌處理,然後才能開始使用它。 – Bootstrapper
我最終使用Android的帳戶選擇器獲取帳戶名稱,並使用GoogleAuthUtil.getToken(Context,accoutName,scope)來檢索令牌本身,它像魅力一樣工作,然後比第一個Approuch簡單得多。 –