简体   繁体   English

你能告诉我这个用 PHP 编写的谷歌驱动 API 调用函数有什么问题吗?

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

I've got this code to run and fetch images from my drive.我有这段代码可以运行并从我的驱动器中获取图像。 But i'm running into a problem every time I run this code.但是每次运行此代码时都会遇到问题。

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

I'm getting this error:我收到此错误:

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

It doesn't really seem illegitimate to me.对我来说,这似乎并不违法。 Perhaps you might able to find the bug.也许你能找到这个错误。 Thanks谢谢

I'll answer your nextPageToken problem using Javascript, just take note of the logic.我将使用 Javascript 回答您的nextPageToken问题,只需注意逻辑即可。 I have two listFile() functions which are identical.我有两个相同的 listFile() 函数。 One executes at initial load, after loading the page, it shows the first 10 of my 100 files.一个在初始加载时执行,加载页面后,它显示我的 100 个文件中的前 10 个。 The other executes each time a button is clicked.每次单击按钮时都会执行另一个。

First function to display the inital 10 files.显示初始 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
          // . . .
        });
      }

Second function: This function is triggered by click of a button named "Next Page" which displays the succeeding files from 11 to N.第二个功能:这个功能是通过点击一个名为“下一页”的按钮触发的,该按钮显示从 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
            //. . .          
          });
  }

It appears that the nextPageToken will be ruled invalid unless you include the exact same query field (q) in the subsequent requests that was included in the initial request.除非您在初始请求中包含的后续请求中包含完全相同的查询字段 (q),否则 nextPageToken 似乎将被裁定为无效。

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

The Google Drive V3 PHP API is not as copiously documented as V2. Google Drive V3 PHP API 的文档不像 V2 那样丰富。

I found no simple PHP examples utilizing pageToken for the V3 API, so I am providing this one:我没有发现将pageToken用于 V3 API 的简单 PHP 示例,因此我提供了以下示例:

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

A V3 tested solution.经过 V3 测试的解决方案。 This will handle large sets (greater then 1000) by handling pagination:这将通过处理分页来处理大集合(大于 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM