我正在尝试访问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一起使用。我做错了什么?
答案 0 :(得分:0)
我找不到“Loopback方式”来做这个,我等不及了,所以我只写了一个简单的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();