Php 使用Ouath访问googledriveapi
我正在尝试学习将Oauth用于我正在运行的web abb。 我试图建立一个基本示例,它应该显示输入的用户的文件列表 然而,我只得到一个空白屏幕。 我已经设法要求它进行身份验证——但我已经做到了这一点 我的代码如下: Index2.php:Php 使用Ouath访问googledriveapi,php,oauth-2.0,google-drive-api,Php,Oauth 2.0,Google Drive Api,我正在尝试学习将Oauth用于我正在运行的web abb。 我试图建立一个基本示例,它应该显示输入的用户的文件列表 然而,我只得到一个空白屏幕。 我已经设法要求它进行身份验证——但我已经做到了这一点 我的代码如下: Index2.php: <?php require_once __DIR__ . '/../vendor/autoload.php'; session_start(); $client = new Google_Client(); $client->setAu
<?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));
}
?>