这就是我使用的代码。
function doc_to_html(id)
{
var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+id+"&exportFormat=html";
var param =
{
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions:true,
};
var html = UrlFetchApp.fetch(url,param).getContentText();
Logger.log(html);
}
它多次用于测试,但现在返回“未授权的错误401”。更准确地说,它正在返回:
<html>
<head>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
我查阅了我能找到的关于UrlFetchApp的所有文档,没有看到任何配额或限制。我检查了脚本作用域,https://www.googleapis.com/auth/script.external_request已被授权。
发布于 2019-05-24 03:29:14
提供的作用域不足以访问文档。要访问它,还需要documents作用域。编辑清单文件使其具有以下作用域:
"oauthScopes": ["https://www.googleapis.com/auth/script.external_request","https://www.googleapis.com/auth/documents"],
或者在脚本中添加对Document App的虚拟调用:
DocumentApp.getActiveDocument();
根据Tanaike的评论,文档作用域似乎还不够,但电子表格作用域也是必需的,即使我们不访问电子表格。或者,需要Drive的只读作用域。
Spreadsheet+Document作用域:
清单:
"oauthScopes": ["https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/documents"
"https://www.googleapis.com/auth/spreadsheets"],
或者在脚本中添加对文档App+SpreadsheetApp的虚拟调用:
//DocumentApp.getActiveDocument();
//SpreadsheetApp.getActive();
驱动器范围:
清单:
"oauthScopes": ["https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/drive.readonly"],
或者在脚本中添加对DriveApp的虚拟调用:
//DriveApp.getFiles()
https://stackoverflow.com/questions/51990923
复制相似问题