Instapaper API 和JavaScript XAuth

发布于 2024-12-06 04:07:22 字数 1576 浏览 0 评论 0原文

我今天大部分时间都在尝试实现 Instapaper 的 XAuth API。我什至还无法获得 oauth 令牌。

知道我做错了什么吗?

我正在使用node.js和oauth模块。据我了解,我需要将用户名、密码、amd 模式作为额外参数传递。 oauth 模块应该处理所有 oauth 参数。但事实并非如此。这是代码:

var OAuth = require('oauth').OAuth;

var oauth = new OAuth(
  '',
  'https://www.instapaper.com/api/1/oauth/access_token',
  'CONSUMER_KEY',
  'CONSUMER_SECRET',
  '1.0',
  null,
  'HMAC-SHA1',
  null
);

var extra = {
  'x_auth_username': 'USERNAME',
  'x_auth_password': 'PASSWORD',
  'x_auth_mode': 'client_auth'
};
var hello = oauth._prepareParameters('', '', 'POST', 'https://www.instapaper.com/api/1/oauth/access_token', null);
var url = 'https://www.instapaper.com/api/1/oauth/access_token';
var f = true;
for (var i in hello) {
  if (f) {
    url += '?';
    f = false;
  } else {
    url += '&';
  }
  url += hello[i][0] + '=' + hello[i][1];
}
console.log(url+'&x_auth_mode=client_auth&x_auth_username=&x_auth_password=')
oauth._performSecureRequest('', '', "POST", url+'&x_auth_mode=client_auth&x_auth_username=&x_auth_password=', null, null, null, function(error, data, response) {
  console.log(error, data)
});

它返回:

{ statusCode: 401,
  data: 'oauth_signature [pWRf4W9k9nogID/O90Ng29bR2K0=] does not match expected value [eqJ8zD1bKeUa3InpDyegGDAbSnM=]' } 'oauth_signature [pWRf4W9k9nogID/O90Ng29bR2K0=] does not match expected value [eqJ8zD1bKeUa3InpDyegGDAbSnM=]'}

I've spent most of today try to implement Instapaper's XAuth API. I haven't even been able to get an oauth token, yet.

Any ideas what I'm doing wrong?

I'm using node.js and the oauth module. It's my understanding that I need to pass the username, password, amd mode as extra parameters. And the oauth module should take care of all of the oauth parameters. But it's not. Here's the code:

var OAuth = require('oauth').OAuth;

var oauth = new OAuth(
  '',
  'https://www.instapaper.com/api/1/oauth/access_token',
  'CONSUMER_KEY',
  'CONSUMER_SECRET',
  '1.0',
  null,
  'HMAC-SHA1',
  null
);

var extra = {
  'x_auth_username': 'USERNAME',
  'x_auth_password': 'PASSWORD',
  'x_auth_mode': 'client_auth'
};
var hello = oauth._prepareParameters('', '', 'POST', 'https://www.instapaper.com/api/1/oauth/access_token', null);
var url = 'https://www.instapaper.com/api/1/oauth/access_token';
var f = true;
for (var i in hello) {
  if (f) {
    url += '?';
    f = false;
  } else {
    url += '&';
  }
  url += hello[i][0] + '=' + hello[i][1];
}
console.log(url+'&x_auth_mode=client_auth&x_auth_username=&x_auth_password=')
oauth._performSecureRequest('', '', "POST", url+'&x_auth_mode=client_auth&x_auth_username=&x_auth_password=', null, null, null, function(error, data, response) {
  console.log(error, data)
});

And it returns this:

{ statusCode: 401,
  data: 'oauth_signature [pWRf4W9k9nogID/O90Ng29bR2K0=] does not match expected value [eqJ8zD1bKeUa3InpDyegGDAbSnM=]' } 'oauth_signature [pWRf4W9k9nogID/O90Ng29bR2K0=] does not match expected value [eqJ8zD1bKeUa3InpDyegGDAbSnM=]'}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑疤怪 2024-12-13 04:07:22

所以我不确定这是否是 oauth 模块的错误,或者 Instapaper 的 API 在解析 Authorization 标头时是否过于严格,但我必须在逗号作为标题分隔符。无论如何,这似乎是导致所有问题的原因(400 个错误)。

oauth 目前构建的标头如下:

oauth_consumer_key=SomeKey,oauth_consumer_secret=SomeSecret...

需要

oauth_consumer_key=SomeKey, oauth_consumer_secret=SomeSecret...

我修改了 oauth.js 文件来反映这一点。
https://github.com/ciaranj/node-oauth /blob/master/lib/oauth.js#L121

在行尾的逗号后面添加了一个空格

authHeader+= "" + this._encodeData(orderedParameters[i][0])+"=\""+ this._encodeData(orderedParameters[i][1])+"\", ";

这是我的工作客户端示例:

var OAuth = require('oauth').OAuth;

var consumerKey    = 'chill';
var consumerSecret = 'duck';

var oa = new OAuth(
  null,
  'https://www.instapaper.com/api/1/oauth/access_token',
  consumerKey,
  consumerSecret,
  '1.0',
  null,
  'HMAC-SHA1'
);

var x_auth_params = {
  'x_auth_mode': 'client_auth',
  'x_auth_password': 'yourpass',
  'x_auth_username': '[email protected]'
};

oa.getOAuthAccessToken(null, null, null, x_auth_params, function (err, token, tokenSecret, results) {

  // CAN HAZ TOKENS!
  console.log(token);
  console.log(tokenSecret);

  // ZOMG DATA!!!
  oa.get("https://www.instapaper.com/api/1/bookmarks/list", token, tokenSecret,  function (err, data, response) {

    console.log(data);

  });

});

希望这会有所帮助!

So I am not sure whether this is an error with the oauth module or if Instapaper's API is too strict in parsing the Authorization headers, but I had to add a space after the comma for the header delimiter. At any rate this seems to be causing all the issues (400 errors).

oauth currently builds up headers as:

oauth_consumer_key=SomeKey,oauth_consumer_secret=SomeSecret...

needed to be

oauth_consumer_key=SomeKey, oauth_consumer_secret=SomeSecret...

I modified the oauth.js file to reflect this.
https://github.com/ciaranj/node-oauth/blob/master/lib/oauth.js#L121

added a space after the comma towards the end of the line

authHeader+= "" + this._encodeData(orderedParameters[i][0])+"=\""+ this._encodeData(orderedParameters[i][1])+"\", ";

Here is my working client sample:

var OAuth = require('oauth').OAuth;

var consumerKey    = 'chill';
var consumerSecret = 'duck';

var oa = new OAuth(
  null,
  'https://www.instapaper.com/api/1/oauth/access_token',
  consumerKey,
  consumerSecret,
  '1.0',
  null,
  'HMAC-SHA1'
);

var x_auth_params = {
  'x_auth_mode': 'client_auth',
  'x_auth_password': 'yourpass',
  'x_auth_username': '[email protected]'
};

oa.getOAuthAccessToken(null, null, null, x_auth_params, function (err, token, tokenSecret, results) {

  // CAN HAZ TOKENS!
  console.log(token);
  console.log(tokenSecret);

  // ZOMG DATA!!!
  oa.get("https://www.instapaper.com/api/1/bookmarks/list", token, tokenSecret,  function (err, data, response) {

    console.log(data);

  });

});

Hope this helps!

魄砕の薆 2024-12-13 04:07:22

Derek 的答案是正确的,因为缺少空格是问题所在,但您不需要编辑 oauth.js

创建OAuth客户端后,只需设置分隔符字符串:(

var OAuth = require('oauth').OAuth;
var oa = new OAuth({...});
oa._oauthParameterSeperator = ', ';

是的,“sepErator”,模块代码中有一个拼写错误)

Derek's answer is right about the missing space as being the problem, but you don't need to edit oauth.js.

After creating the OAuth client, just set the separator string:

var OAuth = require('oauth').OAuth;
var oa = new OAuth({...});
oa._oauthParameterSeperator = ', ';

(yes, "sepErator", there's a typo in the module's code)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文