我正在通过应用程序脚本中的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
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参数吗?
答案 0 :(得分:1)
在评估queryString值时,尝试用&
替换&
。
var queryString = Object.keys(params).map(function(p) {
return [encodeURIComponent(p), encodeURIComponent(params[p])].join("=");
}).join("&");