SHARE
TWEET

google-flow.php

daniel_c05 May 26th, 2014 1,581 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $google_client_id       = 'xxx';
  3. $google_client_secret   = 'xxx';
  4. $google_redirect_url    = 'xxx';
  5. $google_developer_key   = 'xxx';
  6. $google_application_name = 'xxx';
  7. $google_application_scope = 'email'; /* I only needed the basic user info */
  8.  
  9. //include google api files
  10. require_once 'Google/Client.php';
  11. require_once 'Google/Service/Oauth2.php';
  12.  
  13. //start session
  14. session_start();
  15.  
  16. //Create the Client
  17. $gClient = new Google_Client();
  18. // Set Basic Client info as established at the beginning of the file
  19. $gClient->setApplicationName($google_application_name);
  20. $gClient->setClientId($google_client_id);
  21. $gClient->setClientSecret($google_client_secret);
  22. $gClient->setRedirectUri($google_redirect_url);
  23. $gClient->setDeveloperKey($google_developer_key);
  24. $gClient->setScopes($google_application_scope);
  25. //Set this to 'force' in order to get a new refresh_token.
  26. //Useful if you had already granted access to this application.
  27. $gClient->setApprovalPrompt('force');
  28. //Critical in order to get a refresh_token, otherwise it's not provided in the response.
  29. $gClient->setAccessType('offline');
  30.  
  31. $google_oauthV2 = new Google_Service_Oauth2($gClient);
  32.  
  33. /************************************************
  34.   If we're logging out we just need to clear our
  35.   local access token in this case
  36.  ************************************************/
  37. if (isset($_REQUEST['logout'])) {
  38.   unset($_SESSION['access_token']);
  39.   //Perform any other sort of redirection or work.
  40. }
  41.  
  42. /************************************************
  43.   If we have a code back from the OAuth 2.0 flow,
  44.   we need to exchange that with the authenticate()
  45.   function. We store the resultant access token
  46.   bundle in the session, and redirect to ourself.
  47.  ************************************************/
  48. if (isset($_GET['code'])) {
  49.     $gClient->authenticate($_GET['code']);
  50.     $_SESSION['token'] = $gClient->getAccessToken();
  51.     header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL));
  52.     return;
  53. }
  54.  
  55. /************************************************
  56.   If we have an access token, we can make
  57.   requests, else we generate an authentication URL.
  58.  ************************************************/
  59. if (isset($_SESSION['token'])) {
  60.     $gClient->setAccessToken($_SESSION['token']);
  61. }
  62. else {
  63.   $authUrl = $gClient->createAuthUrl();
  64. }
  65.  
  66. /************************************************
  67.   If we're signed in we can go ahead and retrieve
  68.   the user's information.
  69. ************************************************/
  70. if ($gClient->getAccessToken()) {
  71.   //Check if our token has expired.
  72.   if ($gClient->isAccessTokenExpired()) {        
  73.       //Retrieve token from database
  74.       $refreshToken = getRefreshToken($con);
  75.       //Here's where the magical refresh_token comes into play
  76.       $gClient->refreshToken($refreshToken);
  77.   }  
  78.   //Basic User Information
  79.   $user                 = $google_oauthV2->userinfo->get();
  80.   $user_id              = $user['id'];
  81.   $user_name            = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
  82.   $email                = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
  83.   $profile_url          = filter_var($user['link'], FILTER_VALIDATE_URL);
  84.   $profile_image_url    = filter_var($user['picture'], FILTER_VALIDATE_URL);  
  85.  
  86.   $_SESSION['token']    = $gClient->getAccessToken();
  87.   //Save the refresh token on our database.
  88.   $tokens = json_decode($gClient->getAccessToken());
  89.   setRefreshToken($con, $tokens->refresh_token);          
  90. }
  91.  
  92. /************************************************
  93.   Basic user redirects based on whether or not
  94.   they are authenticated.
  95. ************************************************/
  96. if(isset($authUrl)) {
  97.    //If not already on the login page, redirect to the login page.
  98.     if ($_SERVER["REQUEST_URI"] != "/test/clients/login.php") {
  99.       header('Location: http://fatcave.me/test/clients/login.php');  
  100.     }    
  101. }
  102. else {
  103.   //If not already on our main page for authenticated users
  104.   //then let's redirect there.
  105.   if ($_SERVER["REQUEST_URI"] != "/test/clients/") {    
  106.     header('Location: http://fatcave.me/test/clients/');  
  107.   }
  108. }
  109.  
  110. //Simple function to store a given refresh_token on a database
  111. function setRefreshToken ($con, $token) {  
  112.   if (isset($token) && isset($email)) {
  113.     $result = mysqli_query($con,"UPDATE mytable SET refresh_token='" . $token . "'");
  114.   }  
  115. }
  116.  
  117. //Retrieves the refresh_token from our database.
  118. function getRefreshToken ($con) {
  119.   $result = mysqli_query($con,"SELECT refresh_token FROM mytable");  
  120.   $rows = mysqli_num_rows($result);
  121.   if ($rows == 0) {
  122.     return "";
  123.   }
  124.   else {
  125.     $row = mysqli_fetch_array($result);
  126.     return $row['Refresh'];
  127.   }
  128. }
  129. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top