我正在尝试使用网站的API对移动应用程序上的用户进行身份验证。我在我的应用程序上成功登录了Google,因此我尝试通过服务器端进行身份验证。但是,当我将客户端ID传递给requestIdToken
时,会出现12500 ApiException错误。
这是我的代码。
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestServerAuthCode(getString(R.string.server_client_id))
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
mSignInButton = findViewById(R.id.sign_in_button);
mSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
mSignOutButton = findViewById(R.id.sign_out_button);
mSignOutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signOut();
}
});
@Override
protected void onStart() {
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
super.onStart();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void signIn() {
Intent intent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(intent, RC_SIGN_IN);
}
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
updateUI(null);
}
});
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Send the token to the API endpoint and validate
mAuthToken = account.getIdToken();
mAuthCode = account.getServerAuthCode();
login();
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "handleSignInResult:error=" + e.getStatusCode()); // 12500
updateUI(null);
}
}
private void updateUI(GoogleSignInAccount signInAccount) {
if (signInAccount != null) {
mSignInButton.setVisibility(View.GONE);
mSignOutButton.setVisibility(View.VISIBLE);
} else {
mSignInButton.setVisibility(View.VISIBLE);
mSignOutButton.setVisibility(View.GONE);
}
}
我确定我使用的是正确的Web客户端ID。可能是什么原因?