SHARE
TWEET

Untitled

a guest Apr 19th, 2019 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // Enable error reporting
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', 1);
  5.  
  6. $google_redirect_url = 'REDIRECT_URL';
  7.  
  8. //start session
  9. session_start();
  10.  
  11. //include google api files
  12. include_once 'google-api-php-client/src/Google/autoload.php';
  13.  
  14. // New Google client
  15. $gClient = new Google_Client();
  16. $gClient->setApplicationName('ApplicationName');
  17. $gClient->setAuthConfigFile('client_secret.json');
  18. $gClient->addScope(Google_Service_Oauth2::USERINFO_PROFILE);
  19. $gClient->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
  20.  
  21. // New Google Service
  22. $google_oauthV2 = new Google_Service_Oauth2($gClient);
  23.  
  24. // LOGOUT?
  25. if (isset($_REQUEST['logout']))
  26. {
  27.     unset($_SESSION["auto"]);
  28.     unset($_SESSION['token']);
  29.     $gClient->revokeToken();
  30.     header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL)); //redirect user back to page
  31. }
  32.  
  33. // GOOGLE CALLBACK?
  34. if (isset($_GET['code']))
  35. {
  36.     $gClient->authenticate($_GET['code']);
  37.     $_SESSION['token'] = $gClient->getAccessToken();
  38.     header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL));
  39.     return;
  40. }
  41.  
  42. // PAGE RELOAD?
  43. if (isset($_SESSION['token']))
  44. {
  45.     $gClient->setAccessToken($_SESSION['token']);
  46. }
  47.  
  48. // Autologin?
  49. if(isset($_GET["auto"]))
  50. {
  51.     $_SESSION['auto'] = $_GET["auto"];
  52. }
  53.  
  54. // LOGGED IN?
  55. if ($gClient->getAccessToken()) // Sign in
  56. {
  57.     //For logged in user, get details from google using access token
  58.     try {
  59.         $user = $google_oauthV2->userinfo->get();
  60.         $user_id              = $user['id'];
  61.         $user_name            = filter_var($user['givenName'], FILTER_SANITIZE_SPECIAL_CHARS);
  62.         $email                = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
  63.         $gender               = filter_var($user['gender'], FILTER_SANITIZE_SPECIAL_CHARS);
  64.         $profile_url          = filter_var($user['link'], FILTER_VALIDATE_URL);
  65.         $profile_image_url    = filter_var($user['picture'], FILTER_VALIDATE_URL);
  66.         $personMarkup         = "$email<div><img src='$profile_image_url?sz=50'></div>";
  67.         $_SESSION['token']    = $gClient->getAccessToken();
  68.        
  69.         // Show user
  70.         echo '<br /><a href="'.$profile_url.'" target="_blank"><img src="'.$profile_image_url.'?sz=100" /></a>';
  71.         echo '<br /><a class="logout" href="?logout=1">Logout</a>';
  72.        
  73.         $boolarray = Array(false => 'false', true => 'true');
  74.         echo '<p>Was automatical login? '.$boolarray[isset($_SESSION["auto"])].'</p>';
  75.        
  76.         //list all user details
  77.         echo '<pre>';
  78.         print_r($user);
  79.         echo '</pre>';  
  80.     } catch (Exception $e) {
  81.         // The user revoke the permission for this App! Therefore reset session token  
  82.         unset($_SESSION["auto"]);
  83.         unset($_SESSION['token']);
  84.         header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL));
  85.     }
  86. }
  87. else // Sign up
  88. {
  89.     //For Guest user, get google login url
  90.     $authUrl = $gClient->createAuthUrl();
  91.    
  92.     // Fast access or manual login button?
  93.     if(isset($_GET["auto"]))
  94.     {
  95.         header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
  96.     }
  97.     else
  98.     {
  99.         echo '<p>Login?</p>';
  100.         echo '<a class="login" href="'.$authUrl.'"><img src="images/google-login-button.png" /></a>';
  101.     }
  102. }
  103. ?>
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top