Php 使用Google登录时抛出无效的\u授权
使用Google+API登录Google在服务器上不起作用,但在本地主机上工作正常 身份验证后,它不会重定向到domains.php文件。在浏览器栏中获取类似下面的URLPhp 使用Google登录时抛出无效的\u授权,php,google-api,Php,Google Api,使用Google+API登录Google在服务器上不起作用,但在本地主机上工作正常 身份验证后,它不会重定向到domains.php文件。在浏览器栏中获取类似下面的URL site.com/domains/domains.php?code=4%2FsrQHdwzRfgdf4isoIuxgfddgRvhgfdfg0WbQ6eV5gfyj7I8ER 错误:致命错误:未捕获的Google\u AuthException:获取OAuth2访问令牌时出错,消息为:在/home/myjobs/public
site.com/domains/domains.php?code=4%2FsrQHdwzRfgdf4isoIuxgfddgRvhgfdfg0WbQ6eV5gfyj7I8ER
错误:致命错误:未捕获的Google\u AuthException:获取OAuth2访问令牌时出错,消息为:在/home/myjobs/public\u html/domain/login\u中使用\u Google/src/auth/Google\u OAuth2.php“invalid\u grant”
有人告诉我答案吗
代码如下:
<?php
//Include GP config file && User class
include_once 'gpConfig.php';
include_once 'User.php';
if(isset($_GET['code'])){
$gClient->authenticate($_GET['code']);
$_SESSION['token'] = $gClient->getAccessToken();
header('Location: ' . filter_var($redirectURL, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$gClient->setAccessToken($_SESSION['token']);
}
if ($gClient->getAccessToken()) {
//Get user profile data from google
$gpUserProfile = $google_oauthV2->userinfo->get();
//Initialize User class
$user = new User();
//Insert or update user data to the database
$gpUserData = array(
'oauth_provider'=> 'google',
'oauth_uid' => $gpUserProfile['id'],
'first_name' => $gpUserProfile['given_name'],
'last_name' => $gpUserProfile['family_name'],
'email' => $gpUserProfile['email'],
'picture' => $gpUserProfile['picture'],
);
$userData = $user->checkUser($gpUserData);
//Storing user data into session
$_SESSION['userData'] = $userData;
//Render facebook profile data
// if(!empty($userData)){
// $output = '<h1>Google+ Profile Details </h1>';
// $output .= '<img src="'.$userData['picture'].'" width="300" height="220">';
// $output .= '<br/>Google ID : ' . $userData['oauth_uid'];
// $output .= '<br/>Name : ' . $userData['first_name'].' '.$userData['last_name'];
// $output .= '<br/>Email : ' . $userData['email'];
// $output .= '<br/>Logged in with : Google';
// $output .= '<br/>Logout from <a href="logout.php">Google</a>';
// }else{
// $output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
// }
} else {
$authUrl = $gClient->createAuthUrl();
$output = '<a href="'.filter_var($authUrl, FILTER_SANITIZE_URL).'"><img src="images/glogin.png" alt=""/></a>';
echo $output;
}
?>
我不能告诉你为什么它只是在服务器上不工作。每次尝试获取新的访问令牌时,它都应该失败。您正在向getAccesstoken发送访问令牌您应该发送刷新令牌 正在刷新访问令牌。 创建客户端
这里的示例代码致命错误:未捕获的Google\u IOException:HTTP错误:0无法连接到accounts.Google.com端口443:没有在/home/sites/public\u html/domains/login\u with_Google/src/io/Google\u CurlIO.php中进行主机访问的路由。我不知道为什么他们中的许多人会在没有说明原因的情况下投票失败。因为我是一个学习者,我想知道原因。他们中的许多人也面临着同样的问题:
function getOauth2Client() {
try {
$client = buildClient();
// Set the refresh token on the client.
if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
$client->refreshToken($_SESSION['refresh_token']);
}
// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// Set the access token on the client.
$client->setAccessToken($_SESSION['access_token']);
// Refresh the access token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken());
$_SESSION['access_token'] = $client->getAccessToken();
}
return $client;
} else {
// We do not have access request access.
header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
}
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
$client = buildClient();
$client->authenticate($_GET['code']); // Exchange the authencation code for a refresh token and access token.
// Add access token and refresh token to seession.
$_SESSION['access_token'] = $client->getAccessToken();
$_SESSION['refresh_token'] = $client->getRefreshToken();
//Redirect back to main script
$redirect_uri = str_replace("oauth2callback.php",$_SESSION['mainScript'],$client->getRedirectUri());
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));