Google Apps脚本UrlFetchApp返回未授权错误401 [英] Google Apps Script UrlFetchApp returning Unauthorized Error 401

查看:111
本文介绍了Google Apps脚本UrlFetchApp返回未授权错误401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在使用的代码.

This is the code I'm using.

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.更准确地说,它返回:

It worked several times for testing, but now it's returning "Unauthorized Error 401." More accurately, it is returning:

<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 被授权.

I've looked into all the documentation I could find about UrlFetchApp and didn't see any quota or restrictions. I checked the script scope and https://www.googleapis.com/auth/script.external_request is authorized.

推荐答案

提供的范围不足以访问文档.要访问它,您还需要文档作用域.编辑清单文件以具有以下范围:

The scopes provided are insufficient to access the document. To access it, you'd need documents scope as well. Edit the manifest file to have these scopes:

"oauthScopes": ["https://www.googleapis.com/auth/script.external_request","https://www.googleapis.com/auth/documents"],

或在脚本中添加对Document App的虚拟调用:

Or add a dummy call to Document App in your script:

DocumentApp.getActiveDocument();


基于 Tanaike 的评论,似乎文档范围还不够,但是即使是电子表格范围也需要我们不访问电子表格.另外,必须使用云端硬盘的只读作用域.


Based on comments from Tanaike, It seems that Documents scope wasn't enough, but Spreadsheet scope is also needed, even though we don't access spreadsheets. Alternatively, Drive's readonly scope is required.

清单:

"oauthScopes": ["https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/documents"
    "https://www.googleapis.com/auth/spreadsheets"],

或在脚本中添加对Document App + SpreadsheetApp的虚拟调用:

Or add a dummy call to Document App+SpreadsheetApp in your script:

//DocumentApp.getActiveDocument();
//SpreadsheetApp.getActive();

驱动器范围:

清单:

"oauthScopes": ["https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/drive.readonly"],

或在脚本中添加对DriveApp的虚拟调用:

Or add a dummy call to DriveApp in your script:

//DriveApp.getFiles()

这篇关于Google Apps脚本UrlFetchApp返回未授权错误401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆