1

我正在使用 google oauth,它无法打印出电子邮件,我可能在这里遗漏了一些东西。任何帮助,将不胜感激。

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';

session_start();

$client = new Google_Client();
$client->setApplicationName("Google+ PHP Starter Application");
// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.


$client->setClientId('xxxx');
$client->setClientSecret('xxxx');
$client->setRedirectUri('xxxx');
$client->setDeveloperKey('xxxx');
$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email',
                        'https://www.googleapis.com/auth/userinfo.profile',
                        'https://www.googleapis.com/auth/plus.me' ));


$plus = new Google_PlusService($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
  //header('Location:index.php');


}

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']);
}
else
{
header("location: index.php");
}


if ($client->getAccessToken()) {
  $me = $plus->people->get('me');
  $url = filter_var($me['url'], FILTER_VALIDATE_URL);
  $img = filter_var($me['image']['url'], FILTER_VALIDATE_URL);
  $name = filter_var($me['displayName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
  $personMarkup = "<a rel='me' href='$url' style=;color:#FDB900;>$name</a>&nbsp<img src='$img' style='width:6%'>";
  $_SESSION['access_token'] = $client->getAccessToken();

} else {
  $authUrl = $client->createAuthUrl();
}

以上是我的php。我已经能够打印出用户的姓名、图像,但我尤其需要电子邮件。

4

0 回答 0