我正在处理脚本以更改文件夹的所有权。但我仍然遇到了" oAuth错误"。我在代码中遗漏了什么吗?
function getFolders(){
var me = Session.getEffectiveUser().getEmail();
var folders = DocsList.getAllFolders();
for (var i in folders) {
var oldOwnerEmail = (folders[i].getOwner());
if (oldOwnerEmail != me) {
changeFolderOwner(me, folders[i], oldOwnerEmail);
}
}
}
function changeFolderOwner(newOwnerEmail, folder, oldOwnerEmail){
var base = 'https://docs.google.com/feeds/';
var fetchArgs = googleOAuth_('docs', base);
fetchArgs.method = 'POST';
var rawXml = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>"
+"<category scheme='http://schemas.google.com/g/2005#kind' "
+"term='http://schemas.google.com/acl/2007#accessRule'/>"
+"<gAcl:role value='owner'/>"
+"<gAcl:scope type='user' value='"+newOwnerEmail+"'/>"
+"</entry>";
fetchArgs.payload = rawXml;
fetchArgs.contentType = 'application/atom+xml';
Logger.log(folder.getName());
Logger.log(oldOwnerEmail);
Logger.log(folder.getId());
var url = base + encodeURIComponent(oldOwnerEmail) + '/private/full/'+folder.getId()+'/acl?v=3&alt=json';
var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
}
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey("anonymous");
oAuthConfig.setConsumerSecret("anonymous");
return {oAuthServiceName:name, oAuthUseToken:"always"};
}
按下授权后,我收到错误消息&#34; OAuth错误&#34;当我运行调试。在这一行:
var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
答案 0 :(得分:0)
在Google Apps脚本中,除非您尝试连接外部服务,否则通常无需使用OAuth,但此处并非如此。
在这种情况下,您需要使用DriveApp服务(而不是DocsList),该服务具有a Folder class with a setOwner() method。
这样的事情会改变所有可用文件夹的所有权:
var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
folder.setOwner(newOwnerEmailAddress);
}