The below scrip to email a google spreadsheet as a pdf worked perfectly until last week. Now I get this message: "Authorization is required to perform that action". After reading up on it, I understand that Google no longer supports OAuth 1.0.
Can anyone guide me in the right direction towards updating the script for oAuth 2.0?
function EmailSpreadsheetAsPdf() {
// Google spreadsheet (key) + sheet (gid)
var key = "1XJDY-M2oSfIG6AQ3IYv4SwKn_QmPW2m24ZNB38o7vCw";
var gid = "1593627730";
// Email recipient
var emailTo = “testuser@gmail.com";
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]});
}
Google ressources: https://developers.google.com/identity/protocols/OAuth_ref