2

我正在尝试使用 Netbanx API,但我总是得到 {"error":{"code":401,"message":"Not authorised"}} 我不知道我做错了什么。

var url = "https://api.test.netbanx.com/hosted/v1/orders";
$.ajax({
    url: url,
    headers: { 
    "Authorization": "Basic " + btoa("devcentre4157:B-qa2-0-54b6431d-302c021451aabe02869ba82a4a4253d8b2a170d7950d228b021448948677e24be8180f945f1af2b583676c353b9f") 
    },
    type: 'POST',
    dataType: 'jsonp',
    contentType: 'application/json',
    data: "{merchantRefNum:'89983943',currencyCode:'CAD',totalAmount:'10'}",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function (err) {
        console.log(err);
    }
});
4

2 回答 2

1

我验证了您的代码并收到了 401。

凭证很好,我做了 curl 请求,它返回数据

curl -X POST -H "Content-Type: application/json" \
  -u devcentre4157:B-qa2-0-54b6431d-302c021451aabe02869ba82a4a4253d8b2a170d7950d228b021448948677e24be8180f945f1af2b583676c353b9f \
     https://api.test.netbanx.com/hosted/v1/orders \
  -d '{
          "merchantRefNum" : "89983943",
          "currencyCode"   : "CAD",
          "totalAmount"    : 10
      }'  

{"currencyCode":"CAD","id":"27HBQC4JI28QISA1LM","link":[{"rel":"hosted_payment","uri":"https://pay.test.netbanx.com/hosted/v1/payment/53616c7465645f5f9d3670f3f61d1664e3c0db218618a55369145e7577df013ab0691c526e56a445"},{"rel":"self","uri":"https://devcentre4157:B-qa2-0-54b6431d-302c021451aabe02869ba82a4a4253d8b2a170d7950d228b021448948677e24be8180f945f1af2b583676c353b9f@api.test.netbanx.com/hosted/v1/orders/27HBQC4JI28QISA1LM"},{"rel":"resend_callback","uri":"https://devcentre4157:B-qa2-0-54b6431d-302c021451aabe02869ba82a4a4253d8b2a170d7950d228b021448948677e24be8180f945f1af2b583676c353b9f@api.test.netbanx.com/hosted/v1/orders/27HBQC4JI28QISA1LM/resend_callback"}],"merchantRefNum":"89983943","mode":"live","totalAmount":10,"type":"order"}

我使用 DHC chrome 插件进行了另一项检查 - 它也可以。所以我很确定您的 JavaScript 示例存在跨域问题。Netbanx 只是不允许对 API 进行跨域请求。

在此处输入图像描述

于 2015-09-17T20:29:52.357 回答
0

通常在这些情况下,问题在于密钥是如何编码的。复制和粘贴时可能在开头或结尾处有空格。凭据看起来确实有效。

于 2016-12-29T17:51:35.267 回答