简体   繁体   English

如何在Google+中获取用户的电子邮件和生日

[英]How to get user's email and birthday in google+

<?php
/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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('');
 $client->setClientSecret('');
 $client->setRedirectUri('');
 $client->setDeveloperKey('');

$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');
  var_dump($me);

   // These fields are currently filtered through the PHP sanitize filters.
  // See http://www.php.net/manual/en/filter.filters.sanitize.php


  $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'>$name</a><div><img src='$img'></div>";

  $optParams = array('maxResults' => 100);
  $activities = $plus->activities->listActivities('me', 'public', $optParams);

  $activityMarkup = '';
  foreach($activities['items'] as $activity) {
    // These fields are currently filtered through the PHP sanitize filters.
    // See http://www.php.net/manual/en/filter.filters.sanitize.php
    $url = filter_var($activity['url'], FILTER_VALIDATE_URL);
    $title = filter_var($activity['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $content = filter_var($activity['object']['content'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    var_dump($content);
    exit;
    $activityMarkup .= "<div class='activity'><a href='$url'>$title</a><div>$content</div></div>";
  }


  // The access token may have been updated lazily.
  $_SESSION['access_token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
}
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <link rel='stylesheet' href='style.css' />
</head>
<body>
<header><h1>Google+ Sample App</h1></header>
<div class="box">

<?php if(isset($personMarkup)): ?>
<div class="me"><?php print $personMarkup ?></div>
<?php endif ?>

<?php if(isset($activityMarkup)): ?>
<div class="activities">Your Activities: <?php print $activityMarkup ?></div>
<?php endif ?>

<?php
  if(isset($authUrl)) {
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
  } else {
   print "<a class='logout' href='?logout'>Logout</a>";
  }
?>
</div>
</body>
</html>

I am unable to get the user's email id or birthday. 我无法获取用户的电子邮件ID或生日。 Please give the suggestions to get the user's email id and birthday. 请提供建议以获取用户的电子邮件ID和生日。

There are several bits of missing information from your example which may impact the results you're getting. 您的示例中缺少一些信息,可能会影响您获得的结果。 You may wish to start with the quickstart app at https://github.com/googleplus/gplus-quickstart-php and focus on the sign-in button configuration in index.html and the oauth configuration in signin.php. 您可能希望从https://github.com/googleplus/gplus-quickstart-php上的快速启动应用程序开始,并专注于index.html中的登录按钮配置和signin.php中的oauth配置。

In particular, you need to make sure you are requesting the oauth scopes you need in the index.html page. 特别是,您需要确保在index.html页面中请求所需的oauth范围。 You haven't shown this part in your sample above, but to get birthday information (assuming the user has it set), you'll need to request the https://www.googleapis.com/auth/plus.login scope, and to get access to their email address you'll need to request access to the https://www.googleapis.com/auth/userinfo.email and then request the info from the "tokeninfo" endpoint. 您没有在上面的示例中显示此部分,但是要获取生日信息(假设用户已设置),则需要请求https://www.googleapis.com/auth/plus.login范围,并访问他们的电子邮件地址,您需要请求访问https://www.googleapis.com/auth/userinfo.email ,然后从“ tokeninfo”端点请求信息。 See https://developers.google.com/+/api/oauth for more info. 有关更多信息,请参见https://developers.google.com/+/api/oauth

The code sample you're showing also shows the activities.get, not the people.get method. 您显示的代码示例还显示了activity.get,而不是people.get方法。 You may want to post code that illustrates the exact problem. 您可能想发布说明确切问题的代码。 In this case, however, keep in mind that if the person does not make their birthday public, you won't be granted access to this field. 但是,在这种情况下,请记住,如果此人未公开其生日,则不会授予您访问此字段的权限。

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

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