问题描述
在这里新推出StackOverflow和Google Apps脚本。感谢任何帮助/指导。任务:
试图编写一个Google Apps脚本,该脚本会将指定文件夹中所有文件的所有权转让给一个所有者。我是Google Apps专业版帐户的超级用户。但是,我将不是原所有者或新所有者,并且出于安全原因,新所有者不能成为超级管理员(并因此运行脚本)。原始和新的所有者都在同一个域中。
我发现了,我已经使用和调整了我的目的,但我得到请求失败返回代码400.服务器响应:来自URLFetchApp调用的错误。
我做了什么:
通过
我更改了base变量以模拟新文档所有者:
var base ='https:/ /docs.google.com/feeds/';
至
var base = encodeURIComponent('https://docs.google.com/feeds/'+newOwnerEmail+'/private/full');
我还将消费者密钥和密钥更新为googleOAuth_()方法中的正确值。因此,下面是整个代码段,包括有问题的一行:
file.removeEditor(newOwnerEmail);
var base = encodeURIComponent('https://docs.google.com/feeds/'+newOwnerEmail+'/private/full');
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';
var url = base + encodeURIComponent(oldOwnerEmail +'/ private / full /'+ fileId +'/ acl& alt = json');
尝试{var content = UrlFetchApp.fetch(url,fetchArgs).getContentText(); }
catch(err){Logger.log(err.message)}
每次应用程序尝试执行UrlFetchApp.fetch()方法,该应用程序会捕获400错误。
问题:
fetchArgs以某种方式变形(可能是fetchArgs正在馈送的rawXML)?有了新的Google Drive SDK,使用这个API是一个更好的选择吗?我很感谢我可能错过的任何指导或资源,以及改进如何提出这些问题的任何提示。感谢您的帮助。
解决了这个问题。
encodeURIComponent()方法,这显然不需要URL。我还使用了OAuthApp库()开始构建URLFetchApp.fetch()方法的提取选项。我不确定是否只有一个或两个这些修复程序解决了这个问题,但该脚本现在一直运行,所以我是一个快乐的人。
下面的完整更新代码:
function changeOwner(newOwnerEmail,file)
{
var fileId = file.getId();
var oldOwnerEmail = file.getOwner()。getEmail();
if(oldOwnerEmail == newOwnerEmail){return; }
file.removeEditor(newOwnerEmail); //这应该是oldOwnerEmail吗?
var base ='https://docs.google.com/feeds/';
var options = OAuthApp.getAuth('docs');
options.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>;
options.payload = rawXml;
options.contentType ='application / atom + xml';
var API_KEY = getAPIKey();
var url = base + oldOwnerEmail +'/ private / full /'+ fileId +'/ acl?v = 3& alt = json& key ='+ API_KEY
try
{
var result = UrlFetchApp.fetch(url,options);
docsObject = Utilities.jsonParse(result.getContentText());
}
catch(err){Logger.log(err.message)}
} // end changeOwner()
New here to StackOverflow and Google Apps Script. I appreciate any assistance/guidance.
The Task:
I'm trying to write a Google Apps Script that will transfer ownership of all files in a specified folder to one owner. I am a super admin for a Google Apps Premier Edition account. However, I will be neither the original owner or the new owner, and the new owners cannot be super admins (and thus run scripts) for security reasons. Both the original and new owners are on the same domain.
I found the following code, which I've used and tweaked for my purposes, but I'm getting "Request failed for returned code 400. Server response:" errors from the URLFetchApp call.
What I've Done:
Per theGoogle Apps Documents List developers guide I changed the "base" variable to impersonate the new document owner:
var base = 'https://docs.google.com/feeds/';
to
var base = encodeURIComponent('https://docs.google.com/feeds/'+newOwnerEmail+'/private/full');
I also updated the consumer key and secret to the correct values in the googleOAuth_() method. With that, here is the whole section of code leading up and including to the problematic line:
file.removeEditor(newOwnerEmail);
var base = encodeURIComponent('https://docs.google.com/feeds/'+newOwnerEmail+'/private/full');
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';
var url = base + encodeURIComponent(oldOwnerEmail + '/private/full/'+fileId+'/acl&alt=json');
try { var content = UrlFetchApp.fetch(url, fetchArgs).getContentText(); }
catch (err) { Logger.log(err.message) }
Each time the application "tries" to execute the UrlFetchApp.fetch() method, the application "catches" the 400 error.
Questions:
What might I be missing here? Is "fetchArgs" malformed somehow (perhaps the "rawXML" that "fetchArgs" is being fed)? With the new Google Drive SDK, is using this API a better option? I'd appreciate any guidance or resources that I might have missed, and also any tips for improving how to ask these questions. Thanks in advance.
Resolved this issue.
I removed the encodeURIComponent() methods, which is apparently not necessary for the URL. I also used the OAuthApp library (found here) to begin to construct the fetch options for the URLFetchApp.fetch() method. I'm not sure if only one or both of these fixes resolved the issue, but the script consistently works now so I'm a happy person.
Complete updated code below:
function changeOwner(newOwnerEmail, file)
{
var fileId = file.getId();
var oldOwnerEmail = file.getOwner().getEmail();
if (oldOwnerEmail == newOwnerEmail) { return; }
file.removeEditor(newOwnerEmail); //should this be oldOwnerEmail?
var base = 'https://docs.google.com/feeds/';
var options = OAuthApp.getAuth('docs');
options.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>";
options.payload = rawXml;
options.contentType = 'application/atom+xml';
var API_KEY = getAPIKey();
var url = base + oldOwnerEmail+'/private/full/'+fileId+'/acl?v=3&alt=json&key='+API_KEY
try
{
var result = UrlFetchApp.fetch(url, options);
docsObject = Utilities.jsonParse(result.getContentText());
}
catch (err) { Logger.log(err.message) }
}//end changeOwner()
这篇关于在Google文档中更改文档所有者的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!