我在我的网站上使用谷歌+登录。 使用以下语法获取用户信息 - $ 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>';
?>`
答案 0 :(得分:1)
我不确定您是否可以使用$ google_oauthV2对象获取此信息,但您可以通过对Google+ API的人员&gt; get()调用来获取此信息。
您可以使用quickstart-app: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>';
(未经测试但应该有效)