你能告诉我这个 google 驱动器 API 在 PHP 中调用函数有什么问题吗

can you tell me what's wrong with this google drive API call function in PHP

我已将此代码发送到 运行 并从我的驱动器中获取图像。但是我每次 运行 这段代码都会 运行 遇到问题。

  function listF() {

    $result = array();
    $tok = array();
    $nextPageToken = NULL;
  do {
    try {
      $parameters = array();
      if ($nextPageToken) {
        $parameters['pageToken'] = $nextPageToken;
        $parameters['q'] = "mimeType='image/jpeg' or mimeType='image/png'";
      }
      $files = $this->service->files->listFiles($parameters);
      $tok[] = $nextPageToken;
      $result = array_merge($tok, $result, $files->getFiles());
      $nextPageToken = $files->getNextPageToken();
    } catch (Exception $e) {
      print "An error occurred: " . $e->getMessage();
      $nextPageToken = NULL;
    }
  } while ($nextPageToken);
  return $result;
}

我收到这个错误:

An error occurred: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Invalid Value",
    "locationType": "parameter",
    "location": "pageToken"
   }
  ],
  "code": 400,
  "message": "Invalid Value"
 }
}

这对我来说似乎并不违法。也许您可以找到错误。谢谢

我将使用 Javascript 回答您的 nextPageToken 问题,请注意逻辑。 我有两个相同的 listFile() 函数。一个在初始加载时执行,加载页面后,它显示我的 100 个文件中的前 10 个。另一个在每次单击按钮时执行。

第一个函数显示最初的 10 个文件。

//take note of this variable
  var nextToken ;
  function listFiles() {
    gapi.client.drive.files.list({
      'pageSize': 10,
      'fields': "*"
    }).then(function(response) {

          //assign the nextPageToken to a variable to be used later
          nextToken = response.result.nextPageToken;
          // do whatever you like here, like display first 10 files in web page
          // . . .
        });
      }

第二个函数: 此功能通过单击名为 "Next Page" 的按钮触发,该按钮显示从 11 到 N.

的后续文件
 function gotoNextPage(event) {
          gapi.client.drive.files.list({
            'pageSize': 10,
            'fields': "*",
            'pageToken': nextToken
          }).then(function(response) {
            //assign new nextPageToken to make sure new files are displayed
            nextToken = response.result.nextPageToken;
            //display batch of file results in the web page
            //. . .          
          });
  }

似乎 nextPageToken 将被裁定无效,除非您在后续请求中包含与初始请求中包含的完全相同的查询字段 (q)。

var files = []
var nextToken;
gapi.client.drive.files.list({
    'q': "mimeType='image/jpeg' or mimeType='image/png'",   
    'pageSize': 10,
    'fields': 'nextPageToken, files(id, name)'
}).then(function(response) {
    nextToken = response.result.nextPageToken;
    files.push(...response.result.files)
    while (nextToken) {
        gapi.client.drive.files.list({
            'nextPage': nextToken,
            'q': "mimeType='image/jpeg' or mimeType='image/png'",   
            'pageSize': 10,
            'fields': 'nextPageToken, files(id, name)'
        }).then(function(response) {
            nextToken = response.result.nextPageToken;
            files.push(...response.result.files)
        })
    }
});

Google 驱动器 V3 PHP API 没有 V2 那样丰富的文档。

我发现没有简单的 PHP 示例利用 pageToken 用于 V3 API,所以我提供这个:

 $parameters = array();
 $parameters['q'] = "mimeType='image/jpeg' or mimeType='image/png'";
 $parameters['fields'] = "files(id,name), nextPageToken";
 $parameters['pageSize'] = 100;
 $files = $google_drive_service->files->listFiles($parameters);

 /* initially, we're not passing a pageToken, but we need a placeholder value */
 $pageToken = 'go';

 while ($pageToken != null) {
     if (count($files->getFiles()) == 0) {
        echo "No files found.\n";
     } 
     else {
             foreach ($files->getFiles() as $file) {
                echo "name: '".$file->getName()."' ID: '".$file->getId()."'\n";
             }
     }

    /* important step number one - get the next page token (if any) */
    $pageToken = $files->getNextPageToken(); 

    /* important step number two - append the next page token to your query */
    $parameters['pageToken'] = $pageToken;
 
    $files = $google_drive_service->files->listFiles($parameters);
}

经过 V3 测试的解决方案。这将通过处理分页来处理大型集合(大于 1000):

function GetFiles()
{

    $options =
    [
        'pageSize' => 1000,
        'supportsAllDrives' => true,
        'fields' => "files(id, mimeType, name), nextPageToken"
    ];

    $files = [];
    $pageToken = null;

    do
    {
        try
        {
            if ($pageToken !== null)
            {
                $options['pageToken'] = $pageToken;
            }

            $response = $this->service->files->listFiles($options);

            $files = array_merge($files, $response->files);
            $pageToken = $response->getNextPageToken();
        }
        catch (Exception $exception)
        {
            $message = $exception->getMessage();
            echo "Error: $message\r\n";
            $pageToken = null;
        }
    } while ($pageToken !== null);

    return $files;
}