kinlane
11/12/2013 - 10:10 PM

Public Google Spreadsheet to API - oAuth

Public Google Spreadsheet to API - oAuth

<?php
require_once 'config.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$client = new Google_Client();
// Get your credentials from the google console
$client->setClientId($CLIENTID);
$client->setClientSecret($CLIENTSECRET);
$client->setRedirectUri($REDIRECTURL);
$client->setScopes(array('https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'));

$service = new Google_DriveService($client);
$authUrl = $client->createAuthUrl();

$authCode = "";

// Authenticate 
$tokenObject = $client->authenticate($authCode);
$client->setAccessToken($tokenObject);

//echo $tokenObject . "<br />";

$tokens = json_decode($tokenObject);
$refresh_token = $tokens->refresh_token;
$access_token = $tokens->access_token;

echo "Access Token: " . $access_token . "<br />";
echo "Refresh Token: " . $refresh_token . "<br />";

// Get the Users Info to verify.
$request = new Google_HttpRequest("https://www.googleapis.com/oauth2/v2/userinfo?alt=json");
$userinfo = $client->getIo()->authenticatedRequest($request);
$response = $userinfo->getResponseBody();
$response = json_decode($response, true);
$name = $response['name'];
$email = $response['email'];

echo "Name: " . $name . "<br />";
echo "Email: " . $email . "<br />";

// Store these values somewhere!
?>