2017-01-22 139 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'];

希望這個作品!

相關問題