1

我在尝试运行此脚本时收到“OAuth 错误(第 378 行,文件“代码”)错误。

此脚本从 Google 表单收集数据,运行计算(我已删除以节省空间),打开模板电子表格,将数据放入电子表格并以 PDF 格式发送电子邮件(没有网格线)。

所以我有几个问题

- 我应该将我的 Google Apps 登录名和密码放在 oauthConfig.setConsumerKey 和 setConsumerSecret 中吗?我尝试使用“匿名”,但不断收到“需要授权才能执行该操作”错误。

-您是否看到我需要更改以纠正 OAuth 错误?

      var docTemplate = "0AgNhg8MX8TfDdHlrd3VyU0oybWhHSlBPRlU3LWlGaUE";
      var docName = "Motion Control Report";

    function formSubmitCreate(e) {
      //Variables
      //I've removed a bunch of formulas and variables, I have confirmed that all of this is correct

      //Template Info
      var copyId = DocsList.getFileById(docTemplate).makeCopy(docName+' for
'+userName+" "+userTimeStamp).getId();
      var copyDoc = SpreadsheetApp.openById(copyId);
      var copyBody = copyDoc.getActiveSheet();

      copyBody.getRange(1, 2).setValue(userName);
      //Imports a bunch of other values to the spreadsheet, removed to save space

      SpreadsheetApp.flush();

      //Save as PDF and send e-mail
      var pdf = spreadsheetToPDF(copyId);

      var subject = "Motion Control Report - " + userProjectName + " - " +userName;
      var body = userName;
      MailApp.sendEmail(userEmail, subject, body, {bcc: "matt@mocoautomation.com", htmlBody: body, attachments: pdf});

    }

    ////////////////////
    function spreadsheetToPDF(key) {

      var oauthConfig = UrlFetchApp.addOAuthService("spreadsheets");
      var scope = "https://spreadsheets.google.com/feeds"

      oauthConfig.setConsumerKey("myusername");
      oauthConfig.setConsumerSecret("password");
      oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
      oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");

      oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");


      var requestData = {
        "oAuthServiceName": "spreadsheets",
        "oAuthUseToken": "always",
      };
      ///GETTING OAuth ERROR FOR THIS LINE
      var pdf = UrlFetchApp.fetch("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+key+"&fmcmd=12&size=7&fzr=false&portrait=true&fitw=true&locale=en_GB&gid=0&gridlines=false&printtitle=false&sheetnames=false&pagenum=UNDEFINED&attachment=false",
requestData).getBlob().setName("MotionReport");

      return pdf;
    }
4

1 回答 1

1

您不应该在代码中指定您的用户名和密码。要使用的密钥/秘密是anonymous. 替换为以下两行代码 -

 oAuthConfig.setConsumerKey('anonymous');
 oAuthConfig.setConsumerSecret('anonymous');
于 2013-05-21T03:18:52.597 回答