I'm trying to send and email using a Google Doc. This is the code I'm using:
function sendHTMLtemp(){
const id = '1HC3ADjGE24ShOXeK_CYISGGIV7U0scJeXQ8o9oyu6vY';
const url = 'https://docs.google.com/document/d/' id '/export?format=html';
const param ={
mehtod : "get",
headers : {
"Authorization":"Bearer " ScriptApp.getOAuthToken()
},
muteHttpExceptions:true
}
const html = UrlFetchApp.fetch(url,param);
Logger.log(html)
}
But I get this error "Unauthorized Error 401.":
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
CodePudding user response:
When I saw your script, if your Google Apps Script has only your showing script, I think that the scope is only https://www.googleapis.com/auth/script.external_request
. If my guess is correct, I think that in your current situation, the scope might be insufficient. So, please modify your script as follows.
Modified script:
function sendHTMLtemp() {
const id = '1HC3ADjGE24ShOXeK_CYISGGIV7U0scJeXQ8o9oyu6vY';
const url = 'https://docs.google.com/document/d/' id '/export?format=html';
const param = {
mehtod: "get",
headers: {
"Authorization": "Bearer " ScriptApp.getOAuthToken()
},
muteHttpExceptions: true
}
const html = UrlFetchApp.fetch(url, param);
Logger.log(html)
// DriveApp.getFiles(); // <--- This comment line is used for automatically detecting the scope of "https://www.googleapis.com/auth/drive.readonly" by the script editor. So, please don't remove this comment.
}
In this modification, a comment line of
// DriveApp.getFiles();
is used. This comment line is used for automatically detecting the scope ofhttps://www.googleapis.com/auth/drive.readonly
by the script editor. By this, your error can be removed. So, please don't remove this comment.When this modified script is run, a dialog for authorizing the scopes is opened. So, please authorize the scopes. By this, the script is run.
I think that in your situation, the scopes can also be set to the manifest file (
appsscript.json
). But, in this case, the scopes are fixed. I thought that this might not be suitable for your situation. So, I proposed to use the automatic detection of scopes by the script editor.