问题描述
我正在尝试使用GAS中的可选高级参数来获取URL:
I'm trying to fetch a URL using optional advanced parameters in GAS:
function myFunction() {
var options = {};
options = {
headers: {
Authorization: 'Bearer ?????',
'Content-Type': 'application/json',
},
};
UrlFetchApp.fetch(<url>, options);
}
我总是得到:
HTTPResponse:
HTTPResponse:
我在Excel Power Query中进行了相同的提取:
I did the same fetching in Excel Power Query:
let
Source = Json.Document(Web.Contents(<URL>, [Headers=[Authorization="Bearer
?????????????", ContentType="application/json"]])),
它有效,它也可以在Postman中使用...
And it works, it works also using Postman...
GAS有什么问题?请帮我!在此先感谢大家.安德里亚
What's the problem with GAS? Please help me! Thanks in advance to everybody.Andrea
推荐答案
406
错误是因为服务器与客户端之间的内容协商失败.设置 Content-Type
时,Power查询和Postman也可能将接受的返回内容也广告为'application/json'
,但是 UrlFetchApp
不会做同样的事情.添加一个显式的 Accept
标头可以解决此问题:
406
error is because content negotiation between server and client has failed. While setting Content-Type
, It's possible that the accepted return content is also advertised as 'application/json'
by Power query and Postman, but UrlFetchApp
doesn't do the same. Adding a explicit Accept
header solves this:
Accept: 'application/json'
参考文献:
- 406不可接受/Web MDN
- RFC7231
这篇关于Google App脚本外部API返回错误406的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!