Skip to content

Instantly share code, notes, and snippets.

@honsa
Created May 3, 2018 19:36
Show Gist options
  • Save honsa/7683fc413c380fdbaf4d5f473df30858 to your computer and use it in GitHub Desktop.
Save honsa/7683fc413c380fdbaf4d5f473df30858 to your computer and use it in GitHub Desktop.
oauth php
$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->setAccessType("offline"); // offline access
$client->setIncludeGrantedScopes(true); // incremental auth
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth/index.php');
$auth_url = $client->createAuthUrl(); ?>
<button onclick="window.location.href = '<?= $auth_url ?>' ">login</button>
<?php
if(isset($_GET['login'])){
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
}
if(isset($_GET['code'])){
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$client->setAccessToken($_SESSION['access_token']);
$drive = new Google_Service_Drive($client);
$files = $drive->files->listFiles(array())->getFiles();;
echo json_encode($files);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment