obernardovieira

Google+ Connect button

Jan 3rd, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.76 KB | None | 0 0
  1. <?php
  2. require_once 'google-api-php-client/src/Google_Client.php';
  3. require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
  4.  
  5. session_start();
  6.  
  7. $client = new Google_Client();
  8. $client->setApplicationName("Google+ PHP Starter Application");
  9. // Visit https://code.google.com/apis/console to generate your
  10. // oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
  11. $client->setClientId('client_id');
  12. $client->setClientSecret('client_secret');
  13. $client->setRedirectUri('redirect_uri');
  14. $client->setDeveloperKey('developer_key');
  15. $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/plus.me'));// Important!
  16. $plus = new Google_PlusService($client);
  17.  
  18. if (isset($_REQUEST['logout'])) {
  19.   unset($_SESSION['access_token']);
  20. }
  21.  
  22. if (isset($_GET['code'])) {
  23.   $client->authenticate($_GET['code']);
  24.   $_SESSION['access_token'] = $client->getAccessToken();
  25.   header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
  26. }
  27.  
  28. if (isset($_SESSION['access_token'])) {
  29.   $client->setAccessToken($_SESSION['access_token']);
  30. }
  31.  
  32. if ($client->getAccessToken())
  33. {
  34.     $userinfo = $plus->people;
  35.     print_r($userinfo->get('me'));
  36.  
  37. } else
  38. {
  39.     $authUrl = $client->createAuthUrl();
  40. }
  41. ?>
  42. <!doctype html>
  43. <html>
  44. <head>
  45.   <meta charset="utf-8">
  46.   <link rel='stylesheet' href='style.css' />
  47. </head>
  48. <body>
  49. <header><h1>Google+ Sample App</h1></header>
  50. <div class="box">
  51.  
  52. <?php if(isset($personMarkup)): ?>
  53. <div class="me"><?php print $personMarkup ?></div>
  54. <?php endif ?>
  55.  
  56. <?php
  57.   if(isset($authUrl)) {
  58.     print "<a class='login' href='$authUrl'>Connect Me!</a>";
  59.   } else {
  60.    print "<a class='logout' href='?logout'>Logout</a>";
  61.   }
  62. ?>
  63. </div>
  64. </body>
  65. </html>
Add Comment
Please, Sign In to add comment