Skip to content

Instantly share code, notes, and snippets.

@evilnapsis
Last active May 28, 2018 17:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save evilnapsis/df1e1ed2db5d43c72434 to your computer and use it in GitHub Desktop.
Save evilnapsis/df1e1ed2db5d43c72434 to your computer and use it in GitHub Desktop.
<?php
session_start();
if(isset($_GET["logout"])){
session_destroy();
}
require_once 'google-api-php-client/src/Google/autoload.php';
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('TU_CLIENT_ID');
$client->setClientSecret('TU_CLIENT_SECRET');
$client->setRedirectUri('TU_REDIRECT_URI');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$authUrl = $client->createAuthUrl();
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://localhost/examples2/googledrive/step1.php');
}
if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Conectar</a>";
}
if (isset($_SESSION['token'])) {
print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>Salir</a><br>";
$client->setAccessToken($_SESSION['token']);
$service = new Google_Service_Drive($client);
$results = $service->files->listFiles();
if (count($results->getItems()) == 0) {
print "No files found.\n";
} else {
print "<table border='1'><thead><th>Archivo</th><th>Id</th></thead>";
foreach ($results->getItems() as $file) {
printf("<tr><td>%s</td><td> %s</td></tr>", $file->getTitle(), $file->getId());
}
print "</table>";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment