我正在尝试将文件发送到谷歌驱动器
我想用示例来获取文件列表 使用" ID客户端OAuth 2.0"
一旦我采用" tokenID"它只工作一次,我猜它是如何工作的,
我想使用API驱动器只在我的帐户上获取文件
所以我将在Backend服务器中使用
但服务器还有其他类型的API授权 在console.developers.google中,我想这对我的想法很有帮助,但不知道如何使用它
这里的另一个例子我谈到使用令牌
代码上的新更新 我将访问设置为脱机 注意: 仅使用cookie作为示例并进行测试
新问题第一次工作然后当我再次刷新它只显示代码 没有任何结果或错误
<?php
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
include_once "templates/base.php";
session_start();
$client_id = '';
$client_secret = '';
$redirect_uri = '{link}';
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
$client = new Google_Client();
//4/N8jf5YPn4-BwY2J5v30gL5aM-78z2IL7QZMN_TV0odk#
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline');
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setDeveloperKey("{Api server code here}");
if(isset($_COOKIE['code'])||isset($_GET['code']))
{
if(isset($_GET['code'])) {
setcookie('code',$_GET['code']);
echo"Saved code";
}
echo $_COOKIE['code']."<br/>";
$refreshToken=$_COOKIE['code'];
$client->authenticate($refreshToken);
if($client->isAccessTokenExpired()) {
$client->refreshToken($refreshToken);
setcookie('code',$client->getAccessToken());
echo "New token: ".$_COOKIE['code']."<br/>";
}
$service = new Google_Service_Drive($client);
var_dump(retrieveAllFiles($service));
} else
{
$authUrl = $client->createAuthUrl();
echo "<a href='$authUrl'>get code</a>";
}
答案 0 :(得分:0)
我弄明白了我的问题 -i没有注意到我的php配置没有显示错误 -i在重定向链接中获得的代码和我认为相同的“访问令牌”之间混淆了
这是代码
<?php
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
include_once "templates/base.php";
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
session_start();
$client_id = '';
$client_secret = '';
$redirect_uri = '';
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline');
$client->addScope("https://www.googleapis.com/auth/drive");
if(isset($_GET['code']))
{
$code=$_GET['code'];
$client->authenticate($code);
$_SESSION['access_token'] = $client->getAccessToken();
}
if(isset($_SESSION['access_token']))
{
echo " token";
var_dump($_SESSION['access_token']);
$client->setAccessToken($_SESSION['access_token']);
if($client->isAccessTokenExpired()) {
$client->refreshToken(json_decode($_SESSION['access_token'])->refresh_token);
$_SESSION['access_token']=$client->getAccessToken();
echo "new token";
var_dump($_SESSION['access_token']);
}
$service = new Google_Service_Drive($client);
var_dump(retrieveAllFiles($service));
} else
{
$authUrl = $client->createAuthUrl();
echo "<a href='$authUrl'>get code</a>";
}