【发布时间】:2018-04-06 19:39:38
【问题描述】:
我在我的节点服务器中使用了第 3 方 REST API。第 3 方 API 提供商给了我一个 API 密钥和一个 cURL 中的示例,如下所示:
$ curl -u APIKey@https://xyzsite.com/api/v1/users
我不确定我是如何在节点 js 中做到这一点的。我试过跟随但没有运气。我明白了
var options = {
host: "xyzsite.com",
path: "/api/v1/users",
headers: {
"Authorization": "Basic " + myAPIKey
}
};
https.get(options, function(res, error) {
var body = "";
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
console.log(body);
});
res.on('error', function(e) {
console.log(e.message);
});
});
控制台消息
{
"message": "No authentication credentials provided."
}
【问题讨论】:
-
您使用的是什么 API?您没有提供正确的凭据,或者您的请求包含错误。
标签: javascript node.js api rest authentication