Google Drive SDK:从云端硬盘返回的空数组文件 [英] Google Drive SDK: Empty array of files returned from Drive

查看:170
本文介绍了Google Drive SDK:从云端硬盘返回的空数组文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Drive SDK中遇到麻烦。我正在尝试将我的Google云端硬盘帐户中的所有文件都存入我的应用。我觉得我正在做的是Drive SDK文档告诉我要做的一切,但是每次去抓取文件时,我会得到一个返回的空数组,没有错误。我正在使用SDK作为服务帐户,所以设置有点不同。我的网站正在运行drupal。这是我的代码:



1)首先,我构建服务(这是一个名为 buildService()的函数的主体

  $ key = PATH_TO_KEY_FILE; 
if(file_exists($ key)){

尝试{
$ auth = new Google_AssertionCredentials(
SERVICE_ACCOUNT_EMAIL,
数组(DRIVE_SCOPE),
$ key);

$客户端= new Google_Client();
$ client-> setUseObjects(true);
$ client-> setAssertionCredentials($ auth);

} catch(Exception $ e) {
print发生错误:$ e-> getMessage();
}

返回新的Google_DriveService($ client);

}
else {
return找不到密钥文件';
}

这个块完全正常,我从Google_DriveService获得一个对象



2)另一方面,我抓住了服务对象,尝试列出驱动器文件:

  $ parameters = array(); 
$ service = buildService(); //我的功能从步骤1
$ files = retrieveAllFiles($ service,$ parameters);

3)这里是 retrieveAllFiles 函数(直接从SDK文档):

  function retrieveAllFiles($ service,$ parameters){

$ result = array();
$ pageToken = NULL;

do {
try {
if($ pageToken){
$ parameters ['pageToken'] = $ pageToken;
}
$ files = $ service-> files-> listFiles($ parameters);
$ result = array_merge($ result,$ files-> getItems());
$ pageToken = $ files-> getNextPageToken();

} catch(异常$ e){
print发生错误:。 $ E->的getMessage();
$ pageToken = NULL;
}
} while($ pageToken);

return $ result;

}

正如我所说,我收到一个空的结果,任何关于为什么我的结果是空的想法?



任何帮助或建议是非常感谢。

解决方案

在您提供的代码中,您正在访问服务帐户本身的Google云端硬盘,而不是Google云端硬盘的域上的特定用户。这个服务帐户的Google云端硬碟可能是空的,所以得到一个空的文件数组是预期的结果。



如果您要访问Google云端硬盘您的域中的特定用户,并遵循文档,说明如何设置Google Apps全域授权管理你缺少一行:

  $ auth-> prn = $ userEmail; 

这是您设置您尝试冒用的域的用户的电子邮件地址。然后,您将访问该用户的Google云端硬盘数据。


I'm having some trouble with Drive SDK. I am trying to get all files from my Google Drive account into my app. I feel like I am doing everything that the Drive SDK documentation tells me to do, but every time I go to grab files, I get an empty array returned ... and no errors. I'm using the SDK as a service account, so the set up is a bit different. My site is running drupal by the way. Here is my code:

1) first off, I build the service (this is the body of a function named buildService():

$key = PATH_TO_KEY_FILE;
if (file_exists($key)) {

    try {
        $auth = new Google_AssertionCredentials(
                        SERVICE_ACCOUNT_EMAIL,
                        array(DRIVE_SCOPE),
                        $key);

        $client = new Google_Client();
        $client->setUseObjects(true);
        $client->setAssertionCredentials($auth);

    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }

    return new Google_DriveService($client);

}
else{
   return "can't find key file';
}

This block is completely fine, I get an object from Google_DriveService

2) the other side, I grab the service object, and attempt to list Drive files:

$parameters = array();
$service = buildService();  //my function from step 1
$files = retrieveAllFiles($service, $parameters);

3) Here is the the retrieveAllFiles function (straight from the SDK documentation):

function retrieveAllFiles($service, $parameters) {

    $result = array();
    $pageToken = NULL;

    do {
        try { 
            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;

} 

As I said, I am getting an empty result returned, no errors, nothing.

Any Ideas as to why my result is empty?

Any help or suggestions are greatly appreciated.

解决方案

In the code you have provided you are accessing the Google Drive of the Service Account itself and not the Google Drive of a particular user on your domain. It is likely that the Google Drive of the Service Account is empty so getting an empty array of file is the expected result.

If you are trying to access the Google Drive of a particular user on your domain and have followed the documentation explaining how to setup Google Apps domain-wide delegation of Authority you are missing one line:

$auth->prn = $userEmail;

This is where you setup the email fo the user of the domain you are trying to impersonate. Then you will be accessing that user's Google Drive data.

这篇关于Google Drive SDK:从云端硬盘返回的空数组文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆