Answer the question
In order to leave comments, you need to log in
Token validation with Google APIs Client Library for PHP?
Hello! Please help me solve the problem related to token verification on the server side.
In an Android application, I receive a token:
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
String idToken = acct.getIdToken();
...
// TODO(user): Здесь отпралвяю полученный токен на сервер.
} else {
// Show signed-out UI.
}
function getUserFromToken($token) {
$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri('');
$client->setScopes('email');
$client->setAccessToken($token);
$ticket = $client->verifyIdToken();
if ($ticket) {
$data = $ticket->getAttributes();
return $data['payload']['sub']; // user ID
}
return false
}
Answer the question
In order to leave comments, you need to log in
Everything is actually very simple. You have an error in the token verification code. Use the following code:
function getUserFromToken($token) {
$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri('');
$client->setScopes('email');
$user_data = $client->verifyIdToken($access_token);
// Здесь работаете с json для получения конкретных данных
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question