我试图在我的应用程序中集成google plus登录。但是当我调用getServerAuthCode()和getIdToken()时,它总是返回null。我不知道到底是什么问题。
private void handleSignInResult (GoogleSignInResult result)
{
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
Toast.makeText(getApplicationContext(), acct.getDisplayName() + "-" + acct.getEmail() + "-" + acct.getServerAuthCode(), Toast.LENGTH_SHORT).show();
Log.d(TAG, "Server Token google" + acct.getServerAuthCode() + "-ID token" + acct.getIdToken());
create_account_method(acct.getEmail(), acct.getDisplayName(), acct.getServerAuthCode());
} else {
}
}
可能您已经解决了,但供将来参考。我也遇到过同样的问题,而解决方法其实很简单。
在这个链接中阅读getIdToken文档后,我发现当我构建GoogleSignInOptions对象时,它缺少了requesttidtoken (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();
初始化是否正确?试试这个
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);
}
}