【问题标题】:Android get google token idAndroid获取谷歌令牌ID
【发布时间】:2016-05-20 18:10:07
【问题描述】:

我一直在关注这两个教程: https://developers.google.com/identity/sign-in/android/sign-in https://developers.google.com/identity/sign-in/android/backend-auth

我设法将谷歌登录系统集成到我的 android 应用程序中,但是当我尝试获取令牌 id 时,我得到了 null。

当我尝试使用以下代码时:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestIdToken(getString(R.string.server_client_id))
    .build();

我在用户登录时失败。当我打电话时:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

我无法检索令牌。

我尝试将这段代码合并为:

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

但没有任何成功。

这是我的代码:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

 // [START onActivityResult]
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInResult(result);
    }
}
// [END onActivityResult]

// [START handleSignInResult]
private void handleSignInResult(GoogleSignInResult result) {
    Log.d(TAG, "handleSignInResult:" + result.isSuccess());
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acct = result.getSignInAccount();
        mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
        updateUI(true);

        String idToken = acct.getIdToken();
        if (idToken != null) {
            Log.d("TOKEN", idToken);
        } else {
            Log.d("TOKEN", "CHUUUUUUJ WIELKI!");
        }
    } else {
        // Signed out, show unauthenticated UI.
        updateUI(false);
    }
}

【问题讨论】:

  • 以上答案是正确的!谢谢!
  • 很高兴听到。继续前进。
  • 你能帮我解决我的问题吗,我花了几天时间试图解决它...我使用相同的代码,但是当我从谷歌 api 检索 tokenId 时,我得到一个 @987654329 @... ??如果我试图验证它https://www.googleapis.com/oauth2/v3/tokeninfo?access_token= <857 chars response> 我都会得到 'error_description": "Invalid Value' ... 你如何验证你的回复?你也收到String tokenId = <857 chars>吗?

标签: android google-api access-token


【解决方案1】:

Google API 使用 OAuth 2.0 协议进行身份验证和 授权。 Google 支持常见的 OAuth 2.0 场景,例如 那些用于 Web 服务器、已安装和客户端应用程序的应用程序。

首先,从 Google 获取 OAuth 2.0 客户端凭据 开发者控制台。然后您的客户端应用程序请求访问 来自 Google 授权服务器的令牌,从 响应,并将令牌发送到您想要的 Google API 使用权。有关使用 OAuth 2.0 的交互式演示 Google(包括使用您自己的客户端凭据的选项), 试用 OAuth 2.0 Playground。

详情请通过requestIdToken returning null

【讨论】:

  • 你能帮我解决我的问题吗,我花了几天时间试图解决它...我使用相同的代码,但是当我从谷歌 api 检索 tokenId 时,我得到一个 @987654323 @... ??如果我试图验证它https://www.googleapis.com/oauth2/v3/tokeninfo?access_token= <857 chars response> 我都会得到 'error_description": "Invalid Value' ... 你如何验证你的回复?你也收到String tokenId = <857 chars>吗?
  • 嗨,Aleksey!我将首先检查您的开发人员控制台中是否有正确的凭据(SHA-1 指纹),然后仔细检查您是否将开发人员控制台中的详细信息放入代码中。在此处阅读更多信息http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/ 和此处:https://developers.google.com/identity/sign-in/android/start-integrating
猜你喜欢
  • 2017-11-13
  • 1970-01-01
  • 2016-04-19
  • 2013-07-21
  • 1970-01-01
  • 2013-07-20
  • 2018-09-25
  • 2017-04-23
  • 1970-01-01
相关资源
最近更新 更多