0

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

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Urvashi
  • 239
  • 2
  • 10
  • Are you aware that the Google+ API does not support posting programmatically to users profiles? I think that posting to pages requires special beta permission as well. – Linda Lawton - DaImTo Mar 09 '15 at 07:53
  • ok.If you have a solution for above error please post that one also – Urvashi Mar 09 '15 at 10:57
  • Have you seen: http://stackoverflow.com/questions/10025698/authentication-on-google-oauth2-keeps-returning-invalid-grant – Andy Mar 09 '15 at 17:09
  • i am new to google + .first i want login through g+ into my web site .Fro this if someone have sample code pleasepost it . – Urvashi Mar 10 '15 at 12:41
  • Check this answer http://stackoverflow.com/a/38094113/1153703 – Bikesh M Jun 29 '16 at 08:28

0 Answers0