Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store google api (OAuth 2) permissions?

i'm using the examples provided in the "google-api-php-client"-Library (http://code.google.com/p/google-api-php-client/) to implement user login and authorization on my website with google services. I didn't make any changes to the examples, except adding my Client-ID, etc..

The authorization itself works fine: Users can login and i can fetch the provided informations. However, when leaving the page, the whole authorization procedure is called again; users are not remembered and need to grant permissions again, which is some kind of annoying and not typical for google-logins as i know them.

For example: On stackoverflow, i'm logged in with my google account. Whenever i revisit this site, i'm logged in automaticly, or (if logged out) just have to log in again - i do not have to confirm the general rights again. Using the examples on my site however, forces the user to allow access whenever the site is visited again.

Did i make any mistakes, when using the examples? What do i have to do, to avoid the permission request over and over again?

Thanks in advance for any kind of help!

like image 618
Elvis Avatar asked Aug 10 '12 08:08

Elvis


People also ask

Where should OAuth tokens be stored?

Tokens received from OAuth providers are stored in a Client Access Token Store. You can configure client access token stores under the Libraries > OAuth2 Stores node in the Policy Studio tree view.


2 Answers

Use this code for first time to retrieve access_code and save it to database:

<?php
    require 'google-api-php-client/src/Google_Client.php';
    require 'google-api-php-client/src/contrib/Google_DriveService.php';
    require 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
    session_start();

    $client = new Google_Client();
    $client->setClientId(CLIENT_ID);
    $client->setClientSecret(CLIENT_SECRET);
    $client->setRedirectUri(REDIRECT_URI);
    $client->setScopes(array(
      'https://www.googleapis.com/auth/drive',
      'https://www.googleapis.com/auth/userinfo.email',
      'https://www.googleapis.com/auth/userinfo.profile'));

    $client->setUseObjects(true);
    $service = new Google_DriveService($client);
          $client->authenticate();
          $_SESSION['token'] = $client->getAccessToken();
          const ACCESS_TOKEN=$_SESSION['token'];
              //code here to save in database
   ?>

Once ACCESS_TOKEN is saved in database change code to:

<?php
        require 'google-api-php-client/src/Google_Client.php';
        require 'google-api-php-client/src/contrib/Google_DriveService.php';
        require 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
    session_start();

        $client = new Google_Client();
        $client->setClientId(CLIENT_ID);
        $client->setClientSecret(CLIENT_SECRET);
        $client->setRedirectUri(REDIRECT_URI);
        $client->setScopes(array(
          'https://www.googleapis.com/auth/drive',
          'https://www.googleapis.com/auth/userinfo.email',
          'https://www.googleapis.com/auth/userinfo.profile'));

        $client->setUseObjects(true);
        $service = new Google_DriveService($client);

    //ACCESS_TOKEN is already saved in database, is being saved on first time login.

        $_SESSION['access_token'] = ACCESS_TOKEN;

        if (isset($_SESSION['access_token'])) {
          $client->setAccessToken($_SESSION['access_token']);
        }

        if ($client->getAccessToken()) 
        {
           $userinfo = $service->about->get();
           echo '<script>console.log('.json_encode($userinfo).');</script>';

           $userinfoService = new Google_OAuth2Service($client);
           $user = $userinfoService->userinfo->get();
           echo '<script>console.log('.json_encode($user).');</script>';
        } 
    ?>
like image 179
kaushal Avatar answered Sep 29 '22 07:09

kaushal


That works fine for me. Based on the kaushal's answer:

<?php 
require_once 'globals.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$client = new Google_Client();

// Get your credentials from the APIs Console
$client->setClientId('YOUR_ID');
$client->setClientSecret('YOUR_SECRET');
$client->setRedirectUri('REDIRECT_URI');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));


$service = new Google_DriveService($client);
$client->setUseObjects(true);

//if no token in the session
if ($_SESSION['google_token'] == '') {
    //get stored token from DB
    $sToken = $oDb->getOne("SELECT `google_token` FROM `users` WHERE `u_id` = " . (int)$_SESSION['user_id']);
     //if no stored token in DB
    if ($sToken == '') {
        //autentificate user
        $client->authenticate();
        //get new token
        $token = $client->getAccessToken();
        //set token in session
        $_SESSION['google_token'] = $token;
        // set token in DB
        $oDb->Query("UPDATE `users` SET `google_token`='$token' WHERE `u_id` = " . (int)$_SESSION['user_id']);
    } else {
       $_SESSION['google_token'] = $sToken;
    }
}
$client->setAccessToken($_SESSION['google_token']);

//do what you wanna do with clients drive here
?>
like image 27
Iliya Kolev Avatar answered Sep 29 '22 08:09

Iliya Kolev