How to store google api permissions (OAuth 2)?

I use the examples provided in "google-api-php-client" -Library (http://code.google.com/p/google-api-php-client/) to implement the user login and authorization on my site with using google services. I have not made any changes to the examples, except for adding my client ID, etc.

Authorization itself works fine: users can log in, and I can get the information provided. However, when you exit the page, the entire authorization procedure is repeated again; users are not remembered and must again grant permissions, which is something like annoying and not typical for google logins, as I know them.

For example: In stackoverflow, I logged in with my google account. Whenever I review this site, I logged in automatically or (if I logged out) I just need to log in again - I no longer need to check the general rights. However, the use of examples on my site forces the user to allow access whenever the site is visited again.

Were there any errors when using the examples? What do I need to do to repeat the permission request again and again?

Thanks in advance for your help!

+7
source share
3 answers

Use this code for the first time to get access_code and save it to the 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 the 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>'; } ?> 
+1
source

This works great for me. Based on 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 ?> 
+1
source

The Google Drive SDK documentation includes a complete PHP example application that you can use as a reference to get started:

https://developers.google.com/drive/examples/php

Basically, as soon as the user logs in and you get the access token and update the token, you store these credentials in the database and reuse them, rather than asking the user for authentication each time.

0
source

All Articles