1

我在我的网站上使用 google+ 登录。m 使用此语法获取用户信息 -$google_oauthV2->userinfo->get(); 但我没有得到用户的生日。如果可能的话,我应该怎么做才能获得用户的生日和位置。这是我的代码:

`

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

session_start();
$gClient = new Google_Client();
$gClient->setApplicationName('Login');
$gClient->setClientId('client_id');
$gClient->setClientSecret('secret_key');
$gClient->setRedirectUri('redirect_uri');
$gClient->setDeveloperKey('developer_key');
$gClient->setApprovalPrompt('auto');
$google_oauthV2 = new Google_Oauth2Service($gClient);

if (isset($_REQUEST['reset'])) 
{
unset($_SESSION['token']);
$gClient->revokeToken();

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

header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
return;
}
if (isset($_SESSION['token']))
{
$gClient->setAccessToken($_SESSION['token']);
}
if ($gClient->getAccessToken())
{

$user                 = $google_oauthV2->userinfo->get();
$user_id              = $user['id'];
$user_name            = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
$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);
$personMarkup         = "$email<div><img src='$profile_image_url?sz=50'></div>";
$_SESSION['token']    = $gClient->getAccessToken();
}
else
{
//get google login url
$authUrl = $gClient->createAuthUrl();
}

echo '<html xmlns="http://www.w3.org/1999/xhtml">'; 
echo '<head>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
echo '<title>Login with Google</title>'; 
echo '</head>';
echo '<body>';
echo '<h1>Login with Google</h1>';
if(isset($authUrl)) //user is not logged in, show login button
{
echo '<a class="login" href="'.$authUrl.'">Login</a>';
}
else 
{
echo '<br /><a href="'.$profile_url.'" target="_blank"><img src="'.$profile_image_url.'?sz=50" /></a>';
echo '<br /><a class="logout" href="?reset=1">Logout</a>';

echo '<pre>';
print_r($user);

echo '</pre>';
}

echo '</body></html>';
?>`
4

1 回答 1

1

我不确定您是否可以使用 $google_oauthV2 对象获取此信息,但您可以通过 people->get() 调用 Google+ API 来获取它。

这是一个例子

您可以使用快速入门应用程序:https ://code.google.com/p/google-api-php-client/

并替换这个:

$activities = $plus->activities->listActivities('me', 'public');
print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>';

和:

$person = $plus->people->get('google-id');
print 'Person: <pre>' . print_r($person, true) . '</pre>';

(未经测试,但应该工作)

于 2013-08-01T12:32:14.613 回答