Created
May 3, 2018 19:36
-
-
Save honsa/7683fc413c380fdbaf4d5f473df30858 to your computer and use it in GitHub Desktop.
oauth php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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