【问题标题】:loopback rest connection headers authorization环回休息连接头授权
【发布时间】:2018-05-29 13:28:22
【问题描述】:

我正在尝试在 Loopback 应用程序中访问 Shopify Orders API。我有以下数据源:

"ShopifyRestDataSource": {
  "name": "ShopifyRestDataSource",
  "connector": "rest",
  "operations": [{
    "template": {
      "method": "GET",
      "url": "https://mystore.myshopify.com/admin",
      "headers": {
        "accepts": "application/json",
        "content-type": "application/json"
      }
    },
    "headers": {
      "Authorization": "Basic MzdiOD..."
    },
    "functions": {
      "find": []
    }
  }]
}

然后我尝试一个简单的调用:

var ds = app.dataSources.ShopifyRestDataSource;

ds.find(function(err, response, context) {
  if (err) throw err;
  if (response.error) {
    next('> response error: ' + response.error.stack);
  }
  console.log(response);
  next();
});

我收到以下异常消息:

错误:{"errors":"[API] API 密钥或访问令牌无效(无法识别登录名或密码错误)"} 在回调(/order-api/node_modules/loopback-connector-rest/lib/rest-builder.js:529:21)

Shopify API 通过基本 HTTP 身份验证进行身份验证,我确信我的请求有效,因为相同的数据适用于 curl。我做错了什么?

【问题讨论】:

  • 您是否已经尝试打开环回的底层调试日志以查看幕后发生的情况? DEBUG=loopback:datasource:* node server/server.jsloopback.io/doc/en/lb3/Setting-debug-strings.html
  • 我刚刚用我从服务器返回的错误消息修改了原始帖子 - 基本上缺少身份验证。有或没有 DEBUG 都是一样的。

标签: loopbackjs


【解决方案1】:

我找不到执行此操作的“环回方式”,我等不及了,所以我只写了一个简单的 https 节点调用。我会把它贴在这里,但我不会接受它作为答案。我仍然希望有人能提供正确的答案。

let response;

const options = {
  hostname: 'mystore.myshopify.com',
  port: 443,
  path: '/admin/orders.json',
  method: 'GET',
  auth: `${instance.api_key}:${instance.password}`
};

const req = https.request(options, (res) => {
  res.setEncoding('utf8');

  let body = '';

  res.on('data', function(chunk) {
    body += chunk;
  });

  res.on('end', function() {
    let jsonResponse = JSON.parse(body);

    // application logic goes here

    response = 'ok';
  });
});

req.on('error', (e) => {
  response = e.message;
});

req.end();

【讨论】:

    猜你喜欢
    • 2018-07-17
    • 2012-06-01
    • 1970-01-01
    • 2021-03-04
    • 2012-06-16
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    相关资源
    最近更新 更多