使用Google Oauth 2.0进行身份验证时,保存用户信息(名字,姓氏,电子邮件,性别等)以在其他地方使用并保存在数据库中的正确方法是什么。使用以下代码,我们不断收到这些错误:
1)Google_Auth_Exception'消息' OAuth 2.0访问令牌已过期,并且刷新令牌不可用。刷新令牌不是......
2)Google / Service / Resource.php(208):Google_Auth_OAuth2> sign(Object(Google_Http_Request))
3)Google / Service / Oauth2.php(153):Google_Service_Resource->来电(' get',数组,' Google_Service _...')
4)file_name.php(54):Google_Service_Oauth2_Userinfo_Resource-> get()
真的需要一些帮助,我们将于本周日推出更新
我们正在使用此网站上的Google OAuth 2.0代码 https://github.com/google/google-api-php-client
//include google api files
require_once 'src/Google/Client.php';
require_once 'src/Google/Service/Oauth2.php';
//start session
$client_id = 'xxxxxxxxxxxxx';
$client_secret = 'xxxxxxxxxxx';
$redirect_uri = 'xxxxxxxxxx';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/userinfo.profile");
$client->addScope("https://www.googleapis.com/auth/userinfo.email");
$service = new Google_Service_Oauth2($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['upload_token']);
$client->revokeToken(); //added
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); //redirect user back to page
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['upload_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
$client->setAccessToken($_SESSION['upload_token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['upload_token']);
}
}
if ($client->getAccessToken())
{
//For logged in user, get details from google using access token
$user = $service->userinfo->get();
$user_id = $user['id'];
$user_name = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
$first_name = filter_var($user['given_name'], FILTER_SANITIZE_SPECIAL_CHARS);
$last_name = filter_var($user['family_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);
$gender = filter_var($user['gender'], FILTER_SANITIZE_SPECIAL_CHARS);
$personMarkup = "$email<div><img src='$profile_image_url?sz=50'></div>";
$_SESSION['upload_token'] = $client->getAccessToken();
}
else
{
$authUrl = $client->createAuthUrl();
}
?>