使用Ouath访问Google Drive API

时间:2016-05-26 05:52:28

标签: php oauth-2.0 google-drive-api

我正在努力学习使用Oauth来运行我的网络abb。 我试图设置一个基本示例,该示例应显示输入用户的文件列表。

然而,我只得到一个空白的屏幕。 我设法要求它进行身份验证 - 但这就是我已经走了多远。

我的代码如下: Index2.php:

    <?php
require_once __DIR__ . '/../vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('oauth-credentials.json');
$client->addScope("https://www.googleapis.com/auth/drive");

if (isset($_SESSION['access_token'])) {
 $client->setAccessToken($_SESSION['access_token']);
 $drive_service = new Google_Service_Drive($client);
 $files_list = $drive_service->files->listFiles(array())->getItems();
 echo json_encode($files_list);

    echo "success"
} else {
  $redirect_uri = '*******';
  header('Location: ' . $redirect_uri);
    echo "failure";
}

if ($_GET['logout'] == 1) {
 unset($_SESSION['access_token']);   
}
?>

oauth2callback.php:

<?php
require_once __DIR__ . '/../vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('oauth-credentials.json');
$client->setRedirectUri('*****');
$client->addScope("https://www.googleapis.com/auth/drive");

if (! isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
  #$client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->fetchAccessTokenWithAuthCode($_GET['code']);
  $redirect_uri = '*****';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

?>

0 个答案:

没有答案