简体   繁体   English

PHP 中缺少来自 Google ID 令牌的信息

[英]Missing information from Google ID Token in PHP

I'm using Google OAuth 2.0 for my server-side web app with PHP 5.4.45 and Google API Client PHP Library version 2.1.1.我将 Google OAuth 2.0 用于我的服务器端 Web 应用程序,带有 PHP 5.4.45 和 Google API 客户端 PHP 库版本 2.1.1。

I can successfully exchange the authorization code with the refresh_token , access_token and id_token , I can correctly verify the id_token and extract the user's data from it but some information is missing.我可以成功地与refresh_tokenaccess_tokenid_token交换authorization code ,我可以正确验证 id_token 并从中提取用户数据,但缺少一些信息。

I requested profile and email scopes, which both get correctly displayed in the consent screen and in the user's applications settings, but, while the email is available, all the claims associated with the profile scope are missing from the id_token (name, picture, etc...).我请求了profileemail范围,它们都在同意屏幕和用户的应用程序设置中正确显示,但是,虽然电子邮件可用,但 id_token(姓名、图片等)中缺少与profile范围相关的所有声明……)。

I have tried with different users and the problem persists.我尝试了不同的用户,但问题仍然存在。

This is the code I'm using (just for testing purposes):这是我正在使用的代码(仅用于测试目的):

require_once 'php/libs/google_api_client_library/vendor/autoload.php';

if(!isset($_GET['code'])){
    die("code parameter not set");

}else{
    //exchange 'code' with access token, refresh token and id token
    $data = array(
        'code' => $_GET['code'],
        'client_id' => $MY_CLIENT_ID,
        'client_secret' => $MY_CLIENT_SECRET,
        'redirect_uri' => 'https://www.mywebsite.com/oauth2callback.php',
        'grant_type' => 'authorization_code'
    );
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data)
        )
    );
    $response = file_get_contents('https://accounts.google.com/o/oauth2/token', false, stream_context_create($options));

    if($response){
        $tokens = json_decode($response);

        $client = new Google_Client();

        //validate idToken and get user info
        $idtoken = $client->verifyIdToken($tokens->id_token);

        echo "<h1>ID_TOKEN</h1>";
        var_dump($idtoken);
    }
}

The weirdest thing is that it used to work correctly before, and then it suddenly started behaving like that, and I don't remember making any changes to this code.最奇怪的是,它以前可以正常工作,然后突然开始出现这样的行为,我不记得对这段代码进行了任何更改。

I also noticed that this happens only server-side, with PHP, because everything is fine with the JavaScript API Client Library.我还注意到这只会发生在服务器端,使用 PHP,因为 JavaScript API 客户端库一切正常。

Any ideas of why this is happening?为什么会发生这种情况的任何想法?

Let me know if you need any further information and thanks in advance!如果您需要任何进一步的信息,请告诉我,并提前致谢!

I don't know what exactly was the problem, but with the help of some examples I have found on the internet I used Google_Service_Oauth2 class and edited my code as follows:我不知道到底是什么问题,但是在我在互联网上找到的一些示例的帮助下,我使用了Google_Service_Oauth2类并按如下方式编辑了我的代码:

require_once 'php/libs/google_api_client_library/vendor/autoload.php';

if(!isset($_GET['code'])){
    die("code parameter not set");

}else{
    $client = new Google_Client();
    $client->setClientId($MY_CLIENT_ID);
    $client->setClientSecret($MY_CLIENT_SECRET);
    $client->setApplicationName('MyApplicationName');
    $client->setRedirectUri('https://www.mywebsite.com/oauth2callback.php');
    $oauth = new Google_Service_Oauth2($client);

    $client->authenticate($_GET['code']); //exchange 'code' with access token, refresh token and id token

    $accessToken = $client->getAccessToken();

    $userData = $oauth->userinfo->get(); //get user info

    echo "<h1>USER DATA</h1>";
    var_dump($userData);
}

And now everything works just fine!现在一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM