2016-09-19 96 views
0

我跟着這個指南谷歌登錄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()

+0

你用什麼的訪問令牌?ID標記卡更加安全和後端高性能機制auth。如果需要後端訪問資源,請使用授權碼? –

+0

ID令牌包含您可能使用訪問令牌提取的所有信息:https://developers.google.c om/identity /登錄/ android/backend-auth –

+0

@StevenSoneff:我的應用程序正在使用的後端將不接受ID令牌。我得到它與訪問令牌一起工作。我檢查了後端的代碼,並使用https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=[access_token]驗證令牌。 – Decimal

回答

0

如果你只需要驗證用getIdToken()檢索到的令牌應使用以下網址https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123(注意區別?id_token?access_token你使用,你可以找到有關這方面的here的信息。

+0

感謝您的回覆。我無法更改使用'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token= [access_token]'的後端代碼,但如果我將自己的後端構建爲另一個後端,我將在未來記住這一點項目。 – Decimal

+0

驗證令牌已過期後,我們如何生成新的令牌? – user1282637