我正在嘗試使用 Google Doc 發送和發送電子郵件。這是我正在使用的代碼:
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)
}
但我收到此錯誤“未經授權的錯誤 401。”:
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
uj5u.com熱心網友回復:
當我看到你的腳本時,如果你的 Google Apps 腳本只有你的顯示腳本,我認為范圍只有https://www.googleapis.com/auth/script.external_request. 如果我的猜測是正確的,我認為在你目前的情況下,范圍可能不夠。因此,請按如下方式修改您的腳本。
修改后的腳本:
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.
}
在此修改中,使用了注釋行
// DriveApp.getFiles();。此注釋行用于https://www.googleapis.com/auth/drive.readonly腳本編輯器自動檢測范圍。這樣,您的錯誤可以被洗掉。所以,請不要洗掉此評論。運行此修改后的腳本時,將打開一個用于授權范圍的對話框。所以,請授權范圍。這樣,腳本就運行了。
我認為在您的情況下,范圍也可以設定為清單檔案(
appsscript.json)。但是,在這種情況下,范圍是固定的。我認為這可能不適合您的情況。所以,我建議使用腳本編輯器自動檢測范圍。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/494450.html
