Pages

Sunday, August 4, 2013

CODE TO CREATE A CONNECTION WITH GOOGLE DRIVE API USING PHP AND SAVE AUTHENTICATION TOKEN


CODE TO CREATE A CONNECTION WITH GOOGLE DRIVE API USING PHP AND SAVE AUTHENTICATION TOKEN 

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

session_start();
$client = new Google_Client();
// Get your credentials from the APIs Console
//$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('your client side');
$client->setClientSecret('your client secret id');
$client->setRedirectUri('your client redirect uri');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

////I will address building permit
$authUrl = $client->createAuthUrl();
print "Connect Me!";
echo $authUrl;
//Return of the address with the code that I can autenticarme
if (isset($_GET['code'])) {
  $accessToken = $client->authenticate($_GET['code']);
  
  file_put_contents('conf.json', $accessToken);
  $client->setAccessToken($accessToken);
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}


2 comments: