【发布时间】:2016-01-18 20:05:34
【问题描述】:
这是我第一次使用 Google App Engine,所以我的一些术语可能有点离题,请多多包涵。
我有一些 php 代码在 GAE 之外的 Web 应用程序中运行,它使用 google-api-php-client 库来访问 gdrive 文件夹并创建文件。效果很好。
我正在尝试使用具有域范围委派的项目迁移到 GAE,以便我的应用可以访问/创建我的 gdrive 帐户中的文件。例如,假设我的域是 kennywyland.com,而我使用的 Google 帐户是 kenny@kennywyland.com。
我像这样创建我的 Google 客户端,我从 https://developers.google.com/api-client-library/php/auth/service-accounts 获得:
$client = new Google_Client();
$client->setAuth(new Google_Auth_AppIdentity($client));
$client->getAuth()->authenticateForScope('https://www.googleapis.com/auth/drive');
然后我像这样创建 gdrive 服务:
$service = new Google_Service_Drive($client);
我没有从中得到任何错误,并且我能够执行查询而不会引发错误消息,但是,我得到了完全空的结果集。我的 gdrive 帐户中有很多文件,所以我假设我应该在将以下代码发布到 GAE 并运行它时看到它们(retreiveAllFiles() 函数来自 Google 的代码示例:https://developers.google.com/drive/v2/reference/files/list#try-it) :
$allfiles = retrieveAllFiles($service);
print_r($allfiles);
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;
}
但是,我得到一个空的结果集。 print_r() 打印一个空数组:
Array ( )
我错过了什么?我觉得我的项目/应用程序能够成功访问 gdrive 服务器,但它无法在我的帐户中看到与此工作域关联的任何文件。
【问题讨论】:
标签: php google-app-engine google-drive-api google-oauth