Google Apps脚本中的简单功能:
function driveSearch() {
// Log the name of every file in the user's Drive whose visibility is anyonewithLink or anyonecanfind
var files = DriveApp.searchFiles(
'visibility = "anyoneWithLink" or visibility = "anyoneCanFind"');
while (files.hasNext()) {
var file = files.next();
var owner = file.getOwner().getName();
var sa = file.getSharingAccess();
Logger.log(file.getName());
Logger.log('Owner:'+owner);
Logger.log("SharingAccess:"+sa);
}
}
它想在我的gsuite驱动器中找到共享文件。
但是,它说我没有运行DriveApp
我的权限已正确设置和请求,如下所示:
{
"timeZone": "America/New_York",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": [
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/drive.metadata.readonly"
]
}
错误是
您无权调用DriveApp.searchFiles。所需权限:https://www.googleapis.com/auth/drive(第3行,文件“代码”)
searchFiles
为什么需要删除权限?我非常担心无意删除,我不想要全部内容。还有我在做错什么吗?
最后,是的,我的Gsuite允许使用Google Apps脚本。
答案 0 :(得分:2)
该解决方法如何?在这种解决方法中,UrlFetchApp直接请求Drive API的端点,并使用以下2个作用域。
https://www.googleapis.com/auth/drive.metadata.readonly
https://www.googleapis.com/auth/script.external_request
在使用此脚本之前,请按以下步骤启用Drive API。
function myFunction() {
var baseUrl = "https://www.googleapis.com/drive/v3/files";
var q = 'visibility = "anyoneWithLink" or visibility = "anyoneCanFind"';
var fields = "files(id,name,owners,permissions),nextPageToken";
var pageToken = "";
var results = [];
var params = {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}};
do {
var endpoint = baseUrl + "?pageSize=1000&q=" + encodeURIComponent(q) + "&fields=" + encodeURIComponent(fields) + "&pageToken=" + pageToken;
var res = UrlFetchApp.fetch(endpoint, params);
res = JSON.parse(res.getContentText());
Array.prototype.push.apply(results, res.files);
pageToken = res.nextPageToken || "";
} while (pageToken);
results.forEach(function(e) {
Logger.log(e.name);
Logger.log('Owner: ' + e.owners.map(function(f) {return f.displayName}).join(","));
Logger.log("SharingAccess: " + e.permissions.map(function(f) {return f.id}).join(","));
});
}
如果这不是您想要的结果,我表示歉意。