email - New OAuth2 needed to run script -
i have been using script send google-spreadsheet e-mail when modified. can replace authentication section oauth2 apps script. oauth 1.0 support deprecated in 2012 , scheduled shut down on april 20, 2015.
thanks.
function onopen() { var ss = spreadsheetapp.getactivespreadsheet(); var menuentries = [ {name: "send email", functionname: "sendemail"}]; ss.addmenu("scripts", menuentries); }; function sendemail() { var ssid = spreadsheetapp.getactivespreadsheet().getid(); var sheetname = spreadsheetapp.getactivespreadsheet().getname(); //var email = session.getuser().getemail(); var email = session.geteffectiveuser(); var subject = "this subject"; var body = "this body :)"; var oauthconfig = urlfetchapp.addoauthservice("google"); oauthconfig.setaccesstokenurl("https://www.google.com/accounts/oauthgetaccesstoken"); oauthconfig.setrequesttokenurl("https://www.google.com/accounts/oauthgetrequesttoken?scope=https://spreadsheets.google.com/feeds/"); oauthconfig.setauthorizationurl("https://www.google.com/accounts/oauthauthorizetoken"); oauthconfig.setconsumerkey("anonymous"); oauthconfig.setconsumersecret("anonymous"); var requestdata = {"method": "get", "oauthservicename": "google", "oauthusetoken": "always"}; var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/export?key=" + ssid + "&gid=0&portrait=true" +"&exportformat=xls"; var result = urlfetchapp.fetch(url , requestdata); var contents = result.getcontent(); mailapp.sendemail(email,subject ,body, {attachments:[{filename:sheetname+".xls", content:contents, mimetype:"application//xls"}]}); };
this easy fix. fetch requests requires scope allowing drive access. can force apps script add scope making call driveapp. in fetch add users oauth token header of call.
function onlytoaddthedrivescope(){ driveapp.getrootfolder() } function onopen() { var ss = spreadsheetapp.getactivespreadsheet(); var menuentries = [ {name: "send email", functionname: "sendemail"}]; ss.addmenu("scripts", menuentries); }; function sendemail() { var ssid = spreadsheetapp.getactivespreadsheet().getid(); var sheetname = spreadsheetapp.getactivespreadsheet().getname(); //var email = session.getuser().getemail(); var email = session.geteffectiveuser(); var subject = "this subject"; var body = "this body :)"; var requestdata = {"method": "get", "headers":{"authorization":"bearer "+scriptapp.getoauthtoken()} }; var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/export?key=" + ssid + "&gid=0&portrait=true" +"&exportformat=xls"; var result = urlfetchapp.fetch(url , requestdata); var contents = result.getcontent(); mailapp.sendemail(email,subject ,body, {attachments:[{filename:sheetname+".xls", content:contents, mimetype:"application//xls"}]}); };
Comments
Post a Comment