2017-01-22 62 views
1

我非常简单的代码directly from Google's website无法验证谷歌访问令牌(错误的号码段)

$client = new Google_Client(['client_id' => $CLIENT_ID]); 

$payload = $client->verifyIdToken($id_token); 

if ($payload) { 
    $userid = $payload['sub']; 
    echo $userid; 
} else { 
    // Invalid ID token 
    echo "error"; 
} 

我得到以下错误(S):

<b>Fatal error</b>: Uncaught exception 'UnexpectedValueException' with message 'Wrong number of segments' in /../vendor/firebase/php-jwt/src/JWT.php:79 
Stack trace: 
#0 /../vendor/google/apiclient/src/Google/AccessToken/Verify.php(103): Firebase\JWT\JWT::decode('ya29.GlzbAwEXTe...', '-----BEGIN PUBL...', Array) 
#1 /../vendor/google/apiclient/src/Google/Client.php(713): Google_AccessToken_Verify-&gt;verifyIdToken('ya29.GlzbAwEXTe...', '1074005180734-g...') 
#2 /../pages/auth/session.php(7): Google_Client-&gt;verifyIdToken('ya29.GlzbAwEXTe...') 

有谁知道为什么会这样是什么?

回答

0

传递时,要回答这个问题,因为另一种是太短,模糊使用id_token的代替的access_token。

而是路过profile.getId()返回的ID的,路过googleUser.getAuthResponse().id_token为您id_token(该idPOST要求您使用超过发送用户的ID到您的服务器的域)返回的一个。

一个伟大的小费为任何开发者:如果你认为你做的一切都是你应该做的事情,它是为他们工作,但它不为你工作,那么你没有做你应该做的一切。

1

我有同样的问题,并没有得到任何修复。我不得不改变我提取用户信息的方式。而不是使用$client->verifyIdToken();我所使用的服务类这样:

$authService=new Google_Service_Oauth2($client); 
    if($client->getAccessToken()){ 
     $data=$authService->userinfo->get(); 
    } 

因此,要获得当前用户的电子邮件,我用$email=data['email'];

希望这个作品!

相关问题