Google Plus客户端的Google Plus支持很快就会被弃用。我需要以下代码行的替代方案,但我无法找到任何替代品。
首先,范围:Google Plus范围将被弃用,并且很快就会产生错误。我已经能够在https://developers.google.com/identity/protocols/googlescopes#oauth2v2 找到以下范围
也许解决方案是使用OpenID,但我不知道如何在PHP中集成范围。我目前的范围设置如下:
$client->setScopes(array(Google_Service_Plus::PLUS_ME, Google_Service_Plus::USERINFO_EMAIL, Google_Service_Plus::USERINFO_PROFILE));
其次,我需要在验证响应令牌后获取配置文件信息,但是,除了OpenID有效负载功能之外我唯一知道的功能是:
$plus = new Google_Service_Plus($client); // starts google profile (plus) service
$me = $plus->people->get('me'); // saves account info
所以,最后一个问题是:我可以从Google oAuth系统获得针对Google PHP客户端的OpenID id_token响应吗?如果是这样,我如何声明范围并获取id_token?
提醒一下:要获取配置文件信息和OpenID id_token,您必须执行以下代码行,这将提供Google Plus过去提供的大部分配置文件信息。
$payload = $client->verifyIdToken($id_token);
用于设置范围:
$client->setScopes("email","https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/userinfo.profile");
获取用户信息
$oauth2 = new Google_Service_Oauth2($client);
$userInfo = $oauth2->userinfo->get();
$email = $userInfo["email"];
$name = $userInfo["givenName"];
$surname = $userInfo["familyName"];
$pic = $userInfo["picture"];
//$client->revokeToken();
如果要使用该系统,也可以传递id令牌
$client->setScopes("openid","email","https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/userinfo.profile");
$token = $client->setAccessToken($access_token);
$idtoken= $token["id_token"]
$payload = $client->verifyIdToken($id_token);
if ($payload) {
$email = $payload["email"];
$name = $payload["given_name"];
$surname = $payload["family_name"];
$pic = $payload["picture"];
}
PS:在这里检查弃用的范围:Google + API's Deprecated。仍会使用一些旧的Google+范围调用,以便获取电子邮件或其他类型的数据。