2015-05-24 168 views
0

下面的腳本通過電子郵件發送谷歌電子表格作爲PDF工作完美,直到上週。現在我收到以下消息:「需要授權才能執行該操作」。閱讀後,我知道Google不再支持OAuth 1.0。電子郵件谷歌電子表格失敗授權Oauth 2.0

任何人都可以引導我正確的方向更新oAuth 2.0的腳本?

function EmailSpreadsheetAsPdf() { 

     // Google spreadsheet (key) + sheet (gid) 
    var key = "1XJDY-M2oSfIG6AQ3IYv4SwKn_QmPW2m24ZNB38o7vCw"; 
    var gid = "1593627730"; 

    // Email recipient 
    var emailTo = 「[email protected]"; 
    var subject = 「This is the subject」; 
    var body = 「This is the body「; 
    var filename = 「Example" + ".pdf"; 

    // Make OAuth Connection 
     var oauthConfig = UrlFetchApp.addOAuthService("google"); 
     var scope = "https://docs.google.com/feeds" 
     oauthConfig.setConsumerKey("anonymous"); 
     oauthConfig.setConsumerSecret("anonymous"); 
     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 request = { 
      "method": "GET", 
      "muteHttpExceptions": true, 
      "oAuthServiceName": "google", 
      "oAuthUseToken": "always", 
     }; 

     // Create the PDF using this hack with special option variables in the URL 
     // As of 2/4/14 this seems to be the only way to export PDF with custom options (landscape, no gridlines, etc) 
     // exportFormat = pdf/csv/xls/xlsx 
     // gridlines = true/false 
     // printtitle = true (1)/false (0) 
     // size = legal/letter/ A4 (according to: http://goo.gl/nPrfdj, but doesn't seem to work?? letter only) 
     // fzr (repeat frozen rows) = true/false 
     // portrait = true (1)/false (0) 
     // fitw (fit to page width) = true (1)/false (0) 
     // add gid if to export a particular sheet - 0, 1, 2,.. 

     //define the params URL to fetch 
     var params = "?gid=" + gid + "&fitw=true&exportFormat=pdf&format=pdf&size=A4&portrait=false&sheetnames=false&printtitle=false&gridlines=false"; 

     //fetching file url 
     var blob = UrlFetchApp.fetch("https://docs.google.com/" + "spreadsheets/d/" + key + "/export" + params, request); 
     var pdf = blob.getBlob().setName(filename); 

     // Send the email with attachment 
     GmailApp.sendEmail(emailTo, subject, body, {attachments:[pdf]}); 

    } 

谷歌ressources:https://developers.google.com/identity/protocols/OAuth_ref

+0

這是用於遷移到2.0的文檔:https://developers.google.com/identity/protocols/OAuth_ref#migration。你特別堅持什麼? – HDCerberus

+0

是的,你有什麼嘗試過,沒有工作? (顯示舊代碼不是嘗試) –

+0

我已經在Google Developers Console(我以前從未使用過)中創建過一個新項目,但無法找到「Google登錄API」,正如遷移指南中所述。 – Jazzpa

回答

0

我終於解決了我的問題。我甚至不需要在開發控制檯中設置任何東西,請參閱下面的解決方案。你運行它的第一次,它會促使你授權訪問你的Gmail。我希望這會對某人有所幫助。

function EmailSpreadsheetAsPdf() { 

// Google spreadsheet (key) + sheet (gid) 
var key = "1XJDY-M2oSfIG6AQ3IYv4SwKn_QmPW2m24ZNB38o7vCw"; 
var gid = "1593627730"; 

// Email recipient 
var emailTo = 「[email protected]"; 
var subject = 「This is the subject」; 
var body = 「This is the body「; 
var filename = 「Example" + ".pdf"; 

// Base URL 
var url = "https://docs.google.com/spreadsheets/d/" + key + "/export?"; 

/* Specify PDF export parameters 
From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579 
*/ 

var url_ext = 'exportFormat=pdf&format=pdf'  // export as pdf/csv/xls/xlsx 
+ '&size=letter'      // paper size legal/letter/A4 
+ '&portrait=false'     // orientation, false for landscape 
+ '&fitw=true&source=labnol'   // fit to page width, false for actual size 
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers 
+ '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines 
+ '&fzr=false'       // do not repeat row headers (frozen rows) on each page 
+ '&gid=';        // the sheet's Id 

var token = ScriptApp.getOAuthToken(); 

// Convert worksheet to PDF 
var pdf = UrlFetchApp.fetch(url + url_ext + gid, { 
    headers: { 
    'Authorization': 'Bearer ' + token 
    } 
}); 

// Send email with PDF attachment 
GmailApp.sendEmail(emailTo, subject, body, { 
    attachments:[pdf]  
}); 


}