javascript - Withings API 不适用于 node-oauth

标签 javascript node.js oauth withings

我正在尝试验证对 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/

相关文章:

javascript - 在类方法中使用时,类中的属性未定义

java - 关于 Google Calendar API v3

java - 在 Tyrus Web Socket 中使用 OAuth

javascript - jQuery slider "slide"事件: How do I determine the user's slide direction?

javascript - Meteor WebSocket 连接到 'ws://.../websocket' 失败 : Error during WebSocket handshake: Unexpected response code: 400

javascript - 更新切片中的状态,将对象作为有效负载

javascript - 4.0 中缺少 Gulp 事件

javascript - Sails.js REST Api 验证

ios - Facebook iOS SDK : refreshing an expired access token without presenting a dialog

javascript - 如何创建环境变量来保护我的网站的 Google map API key (或任何其他 secret 值)?