I want to integrate google plus log in into my website using php. For that I used -
<?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();
?>
It returns user id,profile,pic,email etc. but if again I refresh that page it shows:
=Fatal error: Uncaught exception 'Google_AuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_grant'' in D:\xamp\htdocs\socialposting\profile\google\GooglePlus\lib\src\auth\Google_OAuth2.php:115 Stack trace: #0 D:\xamp\htdocs\socialposting\profile\google\GooglePlus\lib\src\Google_Client.php(127): Google_OAuth2->authenticate(Array, NULL) #1 D:\xamp\htdocs\socialposting\profile\google\GooglePlus\login.php(23): Google_Client->authenticate() #2 D:\xamp\htdocs\socialposting\profile\google\GooglePlus\login.php(69): G_login->login() #3 {main} thrown in D:\xamp\htdocs\socialposting\profile\google\GooglePlus\lib\src\auth\Google_OAuth2.php on line 115
how can i solve this error. I also want to post on google+ profile and pages. thanks