“ 401错误代码”使用Google脚本应用获取帖子

发布于 2025-01-27 19:09:55 字数 1261 浏览 2 评论 0原文

大家好,最近我一直在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2025-02-03 19:09:55

我认为您的错误消息“消息”:“未经授权”“标头”:{headers},CHRISG86的评论

另外,从此文档,似乎需要随身携带请求主体content-type:application/json

来自:

var option = {
  "method": "post",
  'payload' : data,
  "headers": {headers},
};

至:

var option = {
  "method": "post",
  "payload": JSON.stringify(data),
  headers, // or "headers": headers
  "contentType": "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:

var option = {
  "method": "post",
  'payload' : data,
  "headers": {headers},
};

To:

var option = {
  "method": "post",
  "payload": JSON.stringify(data),
  headers, // or "headers": headers
  "contentType": "application/json"
};

Note:

  • In this modification, it supposes that the values of "Basic " + Utilities.base64Encode(Username+ ':' + Password), data and url are correct. Please be careful this.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文