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
1 2 3 4 5 6 7 8 9 10 11 12 |
require _DIR_TO_AUTOLOAD_SDK_.'/google_sdk/autoload.php'; // Required Information // OAuth 2.0 $google_client_id = _GOOGLE_CLIENT_ID_; $google_client_secret = _GOOGLE_CLIENT_SECRET_; // Public API-Access $google_redirect_url = 'http://yourdomain.com/google_login/?option=do_login'; // API-key for a server application! $google_developer_key = _GOOGLE_DEVELOPER_KEY_; |
1 2 3 4 5 6 7 |
$gClient = new Google_Client(); $gClient->setAccessType('offline'); $gClient->setApplicationName('Application Name'); $gClient->setClientId($google_client_id); $gClient->setClientSecret($google_client_secret); $gClient->setRedirectUri($google_redirect_url); $gClient->setDeveloperKey($google_developer_key); |
1 2 3 4 5 6 7 8 9 10 |
$gClient->setScopes(array( 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'https://gdata.youtube.com', 'https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/youtubepartner' ) ); |
1 |
$plus = new Google_Service_Plus($gClient); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
session_start(); // A simple check for the login to get a fresh token if($_GET['option'] == "do_login" && empty($_SESSION['google_token'])){ // Check for redirect from Google & Auth if (empty($_GET['code'])) { // Create Auth redirect header('Location: '.$gClient->createAuthUrl()); } else { $gClient->authenticate($_GET['code']); $access_token = $gClient->getAccessToken(); //Store the token in a session or your database for later use $_SESSION['google_token'] = $access_token; } } // You have already logged in if(!empty($_SESSION['google_token'])){ $gClient->setAccessToken($_SESSION['google_token']); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Making sure we have a valid access token if($gClient->getAccessToken()) { try{ // Get your activities $optParams = array('maxResults' => 100); $activities = $plus->activities->listActivities('me', 'public', $optParams); print_r($activities); } } catch (Google_ServiceException $e) { $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); } catch (Google_Exception $e) { $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); } |
I am a full-stack developer. My expertise include:
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."