2013-05-20 104 views
1

當我嘗試運行此腳本時,出現「OAuth錯誤(行378,文件」代碼「)」錯誤。Google Spreadsheets; OAuth錯誤

此腳本從Google窗體收集數據,運行計算(我已刪除以節省空間),打開模板電子表格,將數據放入電子表格和電子郵件中作爲PDF(無網格線)。

所以,我有幾個問題

-Should我可以把我的谷歌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: "[email protected]", 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; 
    } 

回答

1

您不應該在代碼中指定您的用戶名和密碼。使用的鑰匙/祕密是anonymous。替換爲以下兩行代碼 -

oAuthConfig.setConsumerKey('anonymous'); 
oAuthConfig.setConsumerSecret('anonymous'); 
+0

Arun,感謝您的幫助。/n我做了這些更改,現在我收到了這條錯誤消息「Attachment has no content。(line 355,file」Code「)」。 355行是指; 「MailApp.sendEmail(userEmail,subject,body,{bcc:」[email protected]「,htmlBody:body,attachments:pdf});」 pdf是不是正確地來回傳遞或傳遞的? – MattMoco

+0

這是值得打開一個單獨的線程,但你應該使用Logger.log函數來調試你的代碼,看看它是否爲空。 –