“ 401错误代码”使用Google脚本应用获取帖子
大家好,最近我一直在尝试在App脚本中发表帖子,从一个名为Salesbinder(库存系统)的API中,我设法获取并将所有库存数据删除,但是我一直在努力发布并将文档添加到其中并收到错误代码 - >
“截断的服务器响应:{“消息”:“未经授权”,“ url”:“ \/api \ /2.0 \ /documents.json”,“ code”:401}“
< << << /p>
因为我正在使用我可以保证,同样的用户名和密码对身份验证是正确的,如果有人可以帮助我解决问题,将很感激。
这是API Documentaion( https://www.salesbinder.com/api/api/api/documents/add/add/add/add/add /)和我一直在使用的代码。
function posting(){
var Username = "{API KEY}"
var Password = "x"
var headers = {
"Authorization" : "Basic " + Utilities.base64Encode(Username+ ':' + Password)
};
var url ='{API URL}'
var data ={
"document":{
"customer_id": 'a93a9e9a-5837-4ec5-9dc7-47cc8cfd84e4',
"issue_date":"2022-05-09",
"context_id":5,
"document_items":[
{
"quantity":2,
"price":134,
"item_id":" b04993fe-7b17-42a1-b5e5-2d34890794c9"
}
]
},
};
var option = {
"method": "post",
'payload' : data,
"headers": {headers},
};
UrlFetchApp.fetch(url, option);
}
Hi everyone recently i have been trying to do a fetch post in app script, from an api called salesbinder(inventory system), i have managed to fetch and pulls all inventory data down, however i have been struggling to post and add document to it and received an error code ->
"Truncated server response: {"message":"Unauthorized","url":"\/api\/2.0\/documents.json","code":401}"
since I am using the same username and password I can assure that the details are correct for the authentication, would appreciate a lot if anyone could help me to solve the problem.
Here are the api documentaion (https://www.salesbinder.com/api/documents/add/) and the code i have been using.
function posting(){
var Username = "{API KEY}"
var Password = "x"
var headers = {
"Authorization" : "Basic " + Utilities.base64Encode(Username+ ':' + Password)
};
var url ='{API URL}'
var data ={
"document":{
"customer_id": 'a93a9e9a-5837-4ec5-9dc7-47cc8cfd84e4',
"issue_date":"2022-05-09",
"context_id":5,
"document_items":[
{
"quantity":2,
"price":134,
"item_id":" b04993fe-7b17-42a1-b5e5-2d34890794c9"
}
]
},
};
var option = {
"method": "post",
'payload' : data,
"headers": {headers},
};
UrlFetchApp.fetch(url, option);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的错误消息
“消息”:“未经授权”
是“标头”:{headers},
。 CHRISG86的评论 。另外,从此文档,似乎需要随身携带请求主体
content-type:application/json
。来自:
至:
注意:
“ basic” + utilities.base64encode(用户名 +':' + password)的值> url
是正确的。请小心。I think that your error message of
"message":"Unauthorized"
is due to"headers": {headers},
. This has already been mentioned in chrisg86's comment.And also, from this document, it seems that the request body is required to be sent with
Content-Type: application/json
.From:
To:
Note:
"Basic " + Utilities.base64Encode(Username+ ':' + Password)
,data
andurl
are correct. Please be careful this.