使用Google API获取用户生日

时间:2015-01-29 20:06:27

标签: php api get social-media

我正在使用Google API来帮助用户登录我的网站。我可以使用API​​在登录期间提取他们的姓名,地区,个人资料图片,性别和ID,但我无法过生日。

我如何使用API​​开始他们的生日?

代码:

session_start();
$google_client_id       = 'xx';
$google_client_secret   = 'xx';
$google_redirect_url    = 'xx'; 
$google_developer_key   = 'xx';

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

$gClient = new Google_Client();
$gClient->setApplicationName('xx');
$gClient->setClientId($google_client_id);
$gClient->setClientSecret($google_client_secret);
$gClient->setRedirectUri($google_redirect_url);
$gClient->setDeveloperKey($google_developer_key);
$gClient->setScopes(array('email', 'https://www.googleapis.com/auth/plus.login'));
$google_oauthV2 = new Google_Oauth2Service($gClient);
if (isset($_GET['code'])) 
{ 
    $gClient->authenticate($_GET['code']);
    $_SESSION['token'] = $gClient->getAccessToken();
}

if (isset($_SESSION['token'])) 
{ 
    $gClient->setAccessToken($_SESSION['token']);
}
if ($gClient->getAccessToken()) 
{   
      $user                          = $google_oauthV2->userinfo->get();
      $user_google_id                = $user['id'];
      $user_name                     = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
      $user_first_name           = filter_var($user['given_name'], FILTER_SANITIZE_SPECIAL_CHARS);
      $user_last_name                = filter_var($user['family_name'], FILTER_SANITIZE_SPECIAL_CHARS);
      $user_gender               = filter_var($user['gender'], FILTER_SANITIZE_SPECIAL_CHARS);
      $user_country                  = filter_var($user['locale'], FILTER_SANITIZE_SPECIAL_CHARS);
      $user_age                  = filter_var($user['birthday'], FILTER_SANITIZE_SPECIAL_CHARS);
      $user_email                    = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
      $profile_url               = filter_var($user['link'], FILTER_VALIDATE_URL);
      $profile_image_url             = filter_var($user['picture'], FILTER_VALIDATE_URL);
      $user_image                    = "$profile_image_url?sz=150";
      $_SESSION['token']             = $gClient->getAccessToken();  
}

1 个答案:

答案 0 :(得分:1)

见这里:How to specify the scope of Google API to get the birthday。总结:您必须请求具有特定范围的访问令牌,例如https://www.googleapis.com/auth/plus.login,用户必须将他/她的生日信息设置为public