我跟着這個指南谷歌登錄Android版:https://developers.google.com/identity/sign-in/android/sign-in的OAuth2 INVALID_TOKEN
我可以成功登錄,但我不知道我是否應該使用作爲訪問令牌的getIdToken()
或getServerAuthCode()
。
我試圖通過他們兩個,一個在時間,https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=[access_token]但它總是返回:
{ 「錯誤」: 「INVALID_TOKEN」, 「ERROR_DESCRIPTION」: 「無效值」}
這裏是我想是我的代碼的相關部分:
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(getString(R.string.server_client_id))
.requestServerAuthCode(getString(R.string.server_client_id))
.requestScopes(new Scope(Scopes.EMAIL))
.requestScopes(new Scope(Scopes.PROFILE))
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
// I also tried String googleAccessToken = acct.getIdToken();
String googleAccessToken = acct.getServerAuthCode();
...
// Omitted code to make POST request to server
...
} else {
// Signed out, show unauthenticated UI.
}
我一直在谷歌上搜索了幾個小時,但無濟於事。謝謝。
編輯:
對不起,我想我錯過了一步。我要去試試這個:developers.google.com/android/guides/http-auth然後再用GoogleAuthUtil.getToken()
你用什麼的訪問令牌?ID標記卡更加安全和後端高性能機制auth。如果需要後端訪問資源,請使用授權碼? –
ID令牌包含您可能使用訪問令牌提取的所有信息:https://developers.google.c om/identity /登錄/ android/backend-auth –
@StevenSoneff:我的應用程序正在使用的後端將不接受ID令牌。我得到它與訪問令牌一起工作。我檢查了後端的代碼,並使用https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=[access_token]驗證令牌。 – Decimal