Google plus登录getIdToken返回null

时间:2016-08-10 06:52:48

标签: android login

我正在尝试在我的应用中集成google plus登录功能。但是当我调用getServerAuthCode()和getIdToken()时,它总是返回null。我无法弄清楚究竟是什么问题。

order by

2 个答案:

答案 0 :(得分:7)

可能你已经解决但未来参考。我遇到了同样的问题,解决方案实际上很简单。

在阅读了此link中的getIdToken文档后,我发现在构建GoogleSignInOptions对象时,它缺少requestIdToken(serverClientId)和.requestServerAuthCode(serverClientId,false)调用。

以下是代码:

String serverClientId = getResources().getString(R.string.google_server_client_id);

GoogleSignInOptions gso = new GoogleSignInOptions
                .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestIdToken(serverClientId)
                .requestServerAuthCode(serverClientId, false)
                .build();

答案 1 :(得分:1)

您是否正确初始化? 试试这个

onResume() {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestProfile()
            .requestScopes(new Scope(Scopes.PLUS_ME))
            .requestScopes(new Scope(Scopes.PLUS_LOGIN))
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
}

点击登录按钮后

public void gmailLogin(GoogleApiClient mGoogleApiClient) {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
    }
}