繁体   English   中英

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

[英]Missing information from Google ID Token in PHP

我将 Google OAuth 2.0 用于我的服务器端 Web 应用程序,带有 PHP 5.4.45 和 Google API 客户端 PHP 库版本 2.1.1。

我可以成功地与refresh_tokenaccess_tokenid_token交换authorization code ,我可以正确验证 id_token 并从中提取用户数据,但缺少一些信息。

我请求了profileemail范围,它们都在同意屏幕和用户的应用程序设置中正确显示,但是,虽然电子邮件可用,但 id_token(姓名、图片等)中缺少与profile范围相关的所有声明……)。

我尝试了不同的用户,但问题仍然存在。

这是我正在使用的代码(仅用于测试目的):

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);
    }
}

最奇怪的是,它以前可以正常工作,然后突然开始出现这样的行为,我不记得对这段代码进行了任何更改。

我还注意到这只会发生在服务器端,使用 PHP,因为 JavaScript API 客户端库一切正常。

为什么会发生这种情况的任何想法?

如果您需要任何进一步的信息,请告诉我,并提前致谢!

我不知道到底是什么问题,但是在我在互联网上找到的一些示例的帮助下,我使用了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);
}

现在一切正常!

暂无
暂无

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

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