Categories: APIDevelopmentPHP

Google API / PHP SDK / Plus / Youtube & more

Will be adding some basic information about using the current Google API with the PHP SDK, this includes Google+, Youtube and other services. For setting up the actual API access see the Google documentation and Google Developer Console 

Including the SDK (Github)

  1. require _DIR_TO_AUTOLOAD_SDK_.'/google_sdk/autoload.php';
  2.  
  3. // Required Information
  4.  
  5. // OAuth 2.0
  6. $google_client_id = _GOOGLE_CLIENT_ID_;
  7. $google_client_secret = _GOOGLE_CLIENT_SECRET_;
  8.  
  9. // Public API-Access
  10. $google_redirect_url = 'http://yourdomain.com/google_login/?option=do_login';
  11. // API-key for a server application!
  12. $google_developer_key = _GOOGLE_DEVELOPER_KEY_;

Prepare Client

  1. $gClient = new Google_Client();
  2. $gClient->setAccessType('offline');
  3. $gClient->setApplicationName('Application Name');
  4. $gClient->setClientId($google_client_id);
  5. $gClient->setClientSecret($google_client_secret);
  6. $gClient->setRedirectUri($google_redirect_url);
  7. $gClient->setDeveloperKey($google_developer_key);

Set Scope (Available Scopes)

  1. $gClient->setScopes(array(     
  2. 'https://www.googleapis.com/auth/plus.me',
  3. 'https://www.googleapis.com/auth/plus.login',
  4. 'https://www.googleapis.com/auth/userinfo.email',
  5. 'https://www.googleapis.com/auth/userinfo.profile',
  6. 'https://gdata.youtube.com',
  7. 'https://www.googleapis.com/auth/youtube.readonly',            
  8. 'https://www.googleapis.com/auth/youtubepartner'
  9. )
  10. );

Setup Google Plus Client

  1. $plus = new Google_Service_Plus($gClient);

Login

  1.  
  2. // A simple check for the login to get a fresh token
  3. if($_GET['option'] == "do_login" && empty($_SESSION['google_token'])){
  4.  
  5. // Check for redirect from Google & Auth
  6. if (empty($_GET['code'])) {
  7.  
  8.  // Create Auth redirect               
  9.  header('Location: '.$gClient->createAuthUrl());
  10.                  
  11. } else {                 
  12.                  
  13.  $gClient->authenticate($_GET['code']);
  14.  $access_token = $gClient->getAccessToken();  
  15.  //Store the token in a session or your database for later use
  16.  $_SESSION['google_token'] = $access_token;
  17.  
  18. }      
  19. }
  20. // You have already logged in
  21. if(!empty($_SESSION['google_token'])){
  22.  
  23.  $gClient->setAccessToken($_SESSION['google_token']);
  24.  
  25. }
  26.  

Get Google Plus Activity

  1. // Making sure we have a valid access token
  2. if($gClient->getAccessToken()) {
  3. try{
  4. // Get your activities
  5. $optParams = array('maxResults' => 100);
  6. $activities = $plus->activities->listActivities('me', 'public', $optParams);
  7.  
  8. print_r($activities);
  9. }
  10.  
  11. } catch (Google_ServiceException $e) {
  12.         $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
  13.             htmlspecialchars($e->getMessage()));
  14.     } catch (Google_Exception $e) {
  15.         $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
  16.             htmlspecialchars($e->getMessage()));
  17.     }

 

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

Particle Network Animations in Javascript

What are particle animations? Particle network animations in JavaScript typically involve creating visual representations of… Read More

3 days ago

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

1 month ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago