使用getIdToken()和getServerAuthCode()验证Google登录时的OAuth2 invalid_token

时间:2016-09-19 06:37:59

标签: android google-signin

我按照Google登录Android版指南进行操作:https://developers.google.com/identity/sign-in/android/sign-in

我可以成功登录,但我不确定是否应该使用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()再次拍摄

1 个答案:

答案 0 :(得分:0)

如果您只需要验证使用getIdToken()检索到的令牌,则应使用以下网址https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123(注意您使用的差异?id_token?access_token。您可以找到找到有关此here的信息。