我想使用php将google plus登录到我的网站。为此我用过 -
<?php
class G_login {
function lib_include() {
require_once 'lib/src/Google_Client.php';
require 'lib/src/contrib/Google_Oauth2Service.php';
require_once 'lib/src/contrib/Google_PlusService.php';
}
function login(){
session_start();
$this->lib_include();
$api = new Google_Client();
$api->setApplicationName("InfoTuts");
$api->setClientId('XXXXX-2gsbbr0eii1ul6kfkmvqmjao80mlp67b.apps.googleusercontent.com'); // Enter Client ID
$api->setClientSecret('Khi69F8wZZj8XbrujINvTsKg'); // Enter Client Secret
$api->setAccessType('online');
$api->setScopes(array('https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
$api->setRedirectUri('http://localhost/socialposting/profile/google/GooglePlus/login.php'); // Enter redirect URI
$service = new Google_PlusService($api);
$oauth2 = new Google_Oauth2Service($api);
$api->authenticate();
$_SESSION['token'] = $api->getAccessToken();
if (isset($_SESSION['token'])) {
$set_asess_token = $api->setAccessToken($_SESSION['token']);
}
if ($api->getAccessToken()) {
$data = $service->people->get('me');
$user_data = $oauth2->userinfo->get();
//echo "<pre>";print_r($user_data);echo "</pre>";
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with Google Plus Oauth - InfoTuts</title>
<link rel="stylesheet" type="text/css" media="all" href="style.css">
</head>
<div id="userinfo">
<table class="usertable">
<tr>
<td>Your Name: </td>
<td><?php print $user_data['name']; ?></td>
<td rowspan="5" valign="top"><img src="<?php print $user_data['picture']; ?>?sz=80" /></td>
</tr>
<tr>
<td> Your Email</td>
<td><span style="background:#FFFF00;"><?php print $user_data['email']; ?></span></td>
</tr>
<tr>
<td>Your Profile Link</td>
<td><?php print $user_data['link']; ?></td>
</tr>
</table>
</div>
</body>
</html>
<?php
}
}
}
$obj= New G_login();
$obj->login();
?>
它返回用户ID,个人资料,图片,电子邮件等,但如果再次刷新它显示的页面:
=致命错误:在D:\ xamp \ htdocs \ socialposting \ profile \ google \ GooglePlus \ lib \ src \ auth \ Google_OAuth2中显示错误“Google_AuthException”,并显示错误“获取OAuth2访问令牌时出错,消息:'invalid_grant'' .php:115堆栈跟踪:#0 D:\ xamp \ htdocs \ socialposting \ profile \ google \ GooglePlus \ lib \ src \ Google_Client.php(127):Google_OAuth2-&gt; authenticate(Array,NULL)#1 D:\ xamp \ htdocs \ socialposting \ profile \ google \ GooglePlus \ login.php(23):Google_Client-&gt; authenticate()#2 D:\ xamp \ htdocs \ socialposting \ profile \ google \ GooglePlus \ login.php(69):第115行的D:\ xamp \ htdocs \ socialposting \ profile \ google \ GooglePlus \ lib \ src \ auth \ Google_OAuth2.php中的G_login-&gt; login()#3 {main}
我该如何解决这个错误。我还想发布谷歌+个人资料和页面。 感谢