我跟随these instructions在我的后端服务器上使用Android登录。
Android登录起作用了,我得到了一个带有我的服务器客户端id的ID令牌:
// 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)
.requestIdToken(getString(R.string.server_client_id))
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mGoogleSignIn.setSize(SignInButton.SIZE_STANDARD);
mGoogleSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, REQUEST_CODE_SIGN_IN);
}
});这个令牌作为POST请求发送到我的后端服务器(代码无关:它起作用了)。
然后服务器( symfony后端)使用google/apiclient再次对用户进行身份验证,以便验证用户ID并在后端数据库中获取用户信息:
// Get token from user
$id_token = $request->get('id_token');
// Validate token
try {
$options = [
'client_id' => $this->container->getParameter('google_app_id')
];
$client = new \Google_Client($options);
$payload = $client->verifyIdToken($id_token);
if ($payload) {
$user = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(['googleId' => $payload['sub']]);
if ($user != null) {
$data = [
'client_id' => $user->getClient()->getClientId(),
'client_secret' => $user->getClient()->getSecret(),
];
} else {
$data = [
'error' => 'Unknown user',
'message' => 'Please create an account first',
];
}
} else {
$data = [
'error' => 'Invalid token',
];
}
} catch (\Exception $e) {
$data = [
'error' => 'Invalid token',
'details' => $e->getMessage(),
];
}将引发异常,消息为Signature Verification Failed。
在网上搜索后,我找不到这条消息的原因。是什么导致了这种情况?同样的代码在后台用来工作,我只是改变了Android端的一些东西,但与谷歌登录无关。
发布于 2017-08-14 21:28:13
今天早上我用完全相同的代码再试了一次,它的效果很好。我猜这是Google方面的一个暂时的失败(没想到!)
我想更多地了解错误消息,而不是代码中的错误(根本没有错误!)因此,如果有人有更清晰的解释,我会接受他们的回答。
https://stackoverflow.com/questions/45664811
复制相似问题