Why can't I retrieve the users email from Google Plus API after getting permission

一世执手 提交于 2019-12-12 00:45:11

问题


Using the google plus connect API I'm able to ask for permission for the users email but I don't seem to have access to it.

Here is the code I'm using:

require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_PlusService.php';

session_start();

$client = new Google_Client();
$client->setApplicationName("Test Application");
$client->setClientId('XXXXX');
$client->setClientSecret('XXXXX');
$client->setRedirectUri('XXXXX');
$client->setDeveloperKey('XXXXX'); 
$client->setScopes(array('https://www.googleapis.com/auth/plus.login','https://www.googleapis.com/auth/userinfo.email'));

$plus = new Google_PlusService($client);

if (isset($_REQUEST['logout'])) {
   unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
}

if ($client->getAccessToken()) {

  $me = $plus->people->get('me');
  print_r($me);

 }

Any idea how I retrieve the email address after the user has granted permission to get it?


回答1:


There's an open issue for that since a while that you can star.

In the meantime you can use an authenticated request against the OAuth2 Userinfo API instead.

require_once '../../src/contrib/Google_Oauth2Service.php';

(...)

$oauth2Service = new Google_Oauth2Service($client);

(...)

$userinfo = $oauth2Service->userinfo->get();
$email = $userinfo["email"];



回答2:


The email information comes through a different endpoint and requires another request. The following code should work once you're authorized:

$oauth2Service = new Google_Oauth2Service($client); 
$emailinfo = $oauth2Service->userinfo->get(); 
print_r($emailinfo);


来源:https://stackoverflow.com/questions/17074792/why-cant-i-retrieve-the-users-email-from-google-plus-api-after-getting-permissi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!