你能告诉我什么';PHP中的google drive API调用函数有什么问题

你能告诉我什么';PHP中的google drive API调用函数有什么问题,php,google-api,google-drive-api,google-api-php-client,google-drive-android-api,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'] = $nextPage

我有这个代码来运行并从我的驱动器中获取图像。但是每次我运行这段代码都会遇到问题

  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。谢谢

我将使用Javascript回答您的
下一个GetOken
问题,请注意逻辑。 我有两个相同的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
          // . . .
        });
      }
第二个功能: 单击名为“下一页”的按钮可触发此功能,该按钮显示从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
            //. . .          
          });
  }

我将使用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
          // . . .
        });
      }
第二个功能: 单击名为“下一页”的按钮可触发此功能,该按钮显示从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
            //. . .          
          });
  }

除非在初始请求中包含的后续请求中包含完全相同的查询字段(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)
        })
    }
});

除非在初始请求中包含的后续请求中包含完全相同的查询字段(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)
        })
    }
});

googledrivev3phpapi没有V2那么丰富

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

$parameters=array();
$parameters['q']=“mimeType='image/jpeg'或mimeType='image/png'”;
$parameters['fields']=“文件(id、名称),下一个GetOken”;
$parameters['pageSize']=100;
$files=$google\u drive\u service->files->listFiles($parameters);
/*最初,我们不传递pageToken,但需要一个占位符值*/
$pageToken='go';
而($pageToken!=null){
如果(计数($files->getFiles())==0){
回显“未找到任何文件。\n”;
} 
否则{
foreach($files->getFiles()作为$file){
echo“name:”。$file->getName()。“'ID:”。“$file->getId()。”'\n”;
}
}
/*重要步骤1-获取下一页标记(如果有)*/
$pageToken=$files->getNextPageToken();
/*重要步骤二-将下一页标记附加到查询中*/
$parameters['pageToken']=$pageToken;
$files=$google\u drive\u service->files->listFiles($parameters);
}

Google Drive V3 PHP API的文档没有V2丰富

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

$parameters=array();
$parameters['q']=“mimeType='image/jpeg'或mimeType='image/png'”;
$parameters['fields']=“文件(id、名称),下一个GetOken”;
$parameters['pageSize']=100;
$files=$google\u drive\u service->files->listFiles($parameters);
/*最初,我们不传递pageToken,但需要一个占位符值*/
$pageToken='go';
而($pageToken!=null){
如果(计数($files->getFiles())==0){
回显“未找到任何文件。\n”;
} 
否则{
foreach($files->getFiles()作为$file){
echo“name:”。$file->getName()。“'ID:”。“$file->getId()。”'\n”;
}
}
/*重要步骤1-获取下一页标记(如果有)*/
$pageToken=$files->getNextPageToken();
/*重要步骤二-将下一页标记附加到查询中*/
$parameters['pageToken']=$pageToken;
$files=$google\u drive\u service->files->listFiles($parameters);
}

“位置”:“pageToken”
-听起来像是
pageToken
是具有无效值的参数。您是否检查了相应变量包含的内容…?在您的代码中,您正在定义
$nextPageToken=NULL。因此,如果($nextPageToken)
不起作用,则您的条件
。您收到的错误也是如此,即提供的pageToken值无效。@Chinmayjain它故意为空,这样它就不会进入if语句,因为在“$nextPageToken=$files->getNextPageToken();”弹出之前不会有任何nextPageToken。请尝试在调用listFiles之前显示$nextPageToken的值。我怀疑你的
if($nextpGetOken){
是真的,而它应该是假的。另外,“$parameters['q']`不应该在你的if块
“location”:“pageToken”
-听起来像是
pageToken
是具有无效值的参数。您是否检查了相应变量包含的内容…?在您的代码中,您正在定义
$nextPageToken=NULL;
。因此您的条件
如果($nextPageToken)
从未工作过。您收到的错误也是如此,即提供的pageToken值无效。@Chinmayjain它故意为空,这样它就不会进入if语句,因为在“$nextPageToken=$files->getNextPageToken()”之前不会有任何nextPageTokenpops.try在调用listFiles之前显示$nextPageToken的值。我怀疑您的
if($nextPageToken){
是真的,而它应该是假的。还有`$parameters['q']`不应该在你的if Block中嘿,我在第二次搜索中遇到错误。token中有问题。获取它,但api告诉它错误。设法解决此问题:google sux(嘿,我在第二次搜索中遇到错误。token中有问题。获取它,但api告诉它错误。设法解决此问题:google sux(