Instapaper API &Javascript XAuth [英] Instapaper API & Javascript XAuth

查看:54
本文介绍了Instapaper API &Javascript XAuth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

知道我做错了什么吗?

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

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)
});

它返回这个:

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

推荐答案

所以我不确定这是 oauth 模块的错误还是 Instapaper 的 API 在解析 Authorization 标头,但我必须在标头分隔符的逗号后添加一个空格.无论如何,这似乎导致了所有问题(400 个错误).

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 目前将标头构建为:

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

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

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

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': 'yourusername@whatever.com'
};

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);

  });

});

希望这有帮助!

这篇关于Instapaper API &Javascript XAuth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆