我正在尝试验证对 Withings API 的请求使用 node-oauth ,一个广泛用于 Node.js 的 OAuth 模块。 OAuth 流程的所有初始步骤都有效,我能够获取用户 ID、访问 token 和访问 token secret 。但是,当尝试实际使用这些 token 发出经过身份验证的请求时,我收到以下错误之一:
- 2554 错误的操作或错误的网络服务
- 2555 发生未知错误
- 2556 服务未定义
我已通过测试验证我使用的凭据有效 here .是我做错了什么,还是 Withings API 以某种非标准方式实现,这使得它与 node-oauth 不兼容?
var consumerKey = "";
var consumerSecret = "";
var oauth_access_token = "";
var oauth_access_token_secret = "";
var userid = "";
var oauth = require("oauth");
var withings = new oauth.OAuth(
"https://oauth.withings.com/account/request_token",
"https://oauth.withings.com/account/access_token",
consumerKey,
consumerSecret,
"1.0",
null,
"HMAC-SHA1"
);
var url = "http://wbsapi.withings.net/measure?action=getmeas&userid=" + userid;
withings.get(url, oauth_access_token, oauth_access_token_secret, function(error, response) {
console.log(response);
});
输出:
{"status":2554}
最佳答案
我想出了这个。 node-oauth 库假定大多数 API 都希望在 header 中定义 OAuth 参数。但是,OAuth 参数也可以在查询字符串中定义,这就是 Withings 决定实现它的方式。 node-oauth 库为此定义了一个 signUrl
函数,但您必须显式使用它。一旦将 URL 包装在该函数中,问题就解决了。请注意,不需要将访问 token 传递给 get
函数,因为请求已经签名。
var url = withings.signUrl("http://wbsapi.withings.net/measure?action=getmeas&userid=" + userid, oauth_access_token, oauth_access_token_secret);
withings.get(url, null, null, function(error, response) {
console.log(response);
});
关于javascript - Withings API 不适用于 node-oauth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28677508/