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
這是用於遷移到2.0的文檔:https://developers.google.com/identity/protocols/OAuth_ref#migration。你特別堅持什麼? – HDCerberus
是的,你有什麼嘗試過,沒有工作? (顯示舊代碼不是嘗試) –
我已經在Google Developers Console(我以前從未使用過)中創建過一個新項目,但無法找到「Google登錄API」,正如遷移指南中所述。 – Jazzpa