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

时间:2017-09-06 12:12:21

标签: php google-api google-drive-api google-api-php-client google-drive-android-api

我已经运行此代码并从驱动器中获取图像。但是每次运行此代码时我都会遇到问题。

  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"
 }
}

对我来说,这似乎并不合法。也许你可以找到这个bug。感谢

3 个答案:

答案 0 :(得分:2)

我将使用Javascript回答您的_configuration = ConfigurationBuilder.Build()问题,只需记下逻辑。 我有两个相同的listFile()函数。一个在初始加载时执行,加载页面后,它显示我的100个文件中的前10个。每次单击按钮时都会执行另一个。

第一个功能显示初始10个文件。

nextPageToken

第二功能: 单击名为"下一页"的按钮触发此功能。它显示从11到N的后续文件。

//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
          // . . .
        });
      }

答案 1 :(得分:1)

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

http://localhost:8000/homesite/viewstatus/1/

答案 2 :(得分:0)

Google Drive V3 PHP API的记录不如V2丰富。

我发现没有简单的PHP示例为V3 API使用pageToken,所以我提供了以下示例:

 $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);
}