【问题标题】:Script to change owner of a document in Google Docs在 Google Docs 中更改文档所有者的脚本
【发布时间】:2013-01-02 21:43:14
【问题描述】:

StackOverflow 和 Google Apps 脚本的新功能。我感谢任何帮助/指导。

任务:

我正在尝试编写一个 Google Apps 脚本,它将指定文件夹中所有文件的所有权转移给一个所有者。我是 Google Apps 专业版帐户的超级管理员。但是,我既不是原始所有者也不是新所有者,并且出于安全原因,新所有者不能是超级管理员(因此不能运行脚本)。原始所有者和新所有者都在同一个域中。

我找到了 following code,我已经使用它并对其进行了调整,但我收到来自 URLFetchApp 调用的“请求失败,返回代码 400。服务器响应:”错误。

我做了什么:

根据 Google Apps Documents List developers guide我更改了“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');

  try { var content = UrlFetchApp.fetch(url, fetchArgs).getContentText(); }
  catch (err) { Logger.log(err.message) }

应用程序每次“尝试”执行 UrlFetchApp.fetch() 方法时,应用程序都会“捕获”400 错误。

问题:

我可能在这里遗漏了什么? “fetchArgs”是否以某种方式格式错误(可能是“fetchArgs”正在输入的“rawXML”)?使用新的 Google Drive SDK,使用这个 API 是更好的选择吗?我会很感激我可能错过的任何指导或资源,以及改进如何提出这些问题的任何提示。提前致谢。

【问题讨论】:

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


    【解决方案1】:

    解决了这个问题。

    我删除了 encodeURIComponent() 方法,这对于 URL 显然不是必需的。我还使用 OAuthApp 库 (found here) 开始为 URLFetchApp.fetch() 方法构造获取选项。我不确定这些修复中是否只有一个或两个解决了该问题,但该脚本现在一直有效,所以我是一个快乐的人。

    完整的更新代码如下:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多