在应用程序脚本中通过URL提取调用Drive REST API版本3仅提供默认属性

时间:2019-06-24 12:02:23

标签: google-apps-script google-drive-api

我正在通过应用程序脚本中的URL提取调用Drive REST API版本3。我正在调用文件列表方法并查询文件。查询工作正常,但是我只在api的响应中获取文件的默认属性。如果我使用fields参数来获取更多字段,它将被忽略。

   var retVal =[];
    var baseUrl = "https://www.googleapis.com/drive/v3/files";
    var token = ScriptApp.getOAuthToken();

    var options = {
    method: "GET",
    headers: {"Authorization": "Bearer " + token},
   }

 var maxResults = 100;

 var params = {
 q: query, 
 pageSize: maxResults,
 fields: 'nextPageToken,incompleteSearch,files(kind,id,name,mimeType,starred,trashed)',
 };

 do {      

  var queryString = Object.keys(params).map(function(p) {
    return [encodeURIComponent(p), encodeURIComponent(params[p])].join("=");
  }).join("&"); 

  var apiUrl = baseUrl + "?" + queryString;

  Logger.log(apiUrl);

  var response = JSON.parse(UrlFetchApp.fetch( apiUrl,
  options).getContentText());

  //Logger.log(response);

  response.files.forEach(function(fileObj) {       
    retVal.push(fileObj);
  })

    params['pageToken'] = response.nextPageToken;

 } while (params.pageToken);

 Logger.log(retVal);
 return retVal

编码查询:https://www.googleapis.com/drive/v3/files?q=name%20contains%20%22Test%20%2F%20Blub%2033%22%20and%20not%20mimeType%20%3D%20%22application%2Fvnd.google-apps.folder%22%20and%20trashed%20%3D%20false&pageSize=100&fields=nextPageToken%2CincompleteSearch%2Cfiles(kind%2Cid%2Cname%2CmimeType%2Cstarred%2Ctrashed)&orderBy=folder%2CmodifiedTime%20desc%2Ctitle&supportsTeamDrives=false&includeTeamDriveItems=false

API的

测试结果:[{kind = drive#file,name = Kopie von Test / Blub 33,id = 1oTbd78Bn7R7Xjo6TEAAyZmE5CjwdgRMT,mimeType = application / json},{kind = drive#file,name = Test / Blub 33, id = 12IpttBvSY-Z31ueNqG_Dmb46dXH5udcl,mimeType = application / json},{kind = drive#file,name = Test / Blub 33,id = 1FqKyDFT0bpp1JuAj3WeSV6AL-b12X4vb,mimeType = application / json}]

有人可以帮忙,为什么api会忽略fields参数吗?

1 个答案:

答案 0 :(得分:1)

在评估queryString值时,尝试用&替换&

var queryString = Object.keys(params).map(function(p) {
   return [encodeURIComponent(p), encodeURIComponent(params[p])].join("=");
}).join("&");