Instapaper API & Javascript XAuth
Asked Answered
I

2

7

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=]'}
Inane answered 22/9, 2011 at 17:15 Comment(2)
I'm still banging my head against this. Please help.Inane
Did you ever get this working? Posting a working solution, or marking a solution as the correct answer would be greatMimeograph
A
5

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!

Auster answered 10/3, 2012 at 8:29 Comment(2)
Is this still a "working" sample? Seems like it doesn't work, since the signature for the "getOAuthAccessToken" has since changed, unless there is something I'm missing.Mimeograph
Paul (and everyone else who's facing the same issue like I was), you can use getOAuthRequestToken(x_auth_params, callback) instead (tested with node-oauth v0.9.8). The fix with missing spaces in the Authorization header is still necessary. Make sure to set the requestUrl when creating a new OAuth instance as well, though (to https://www.instapaper.com/api/1/oauth/access_token).Yarborough
S
1

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)

Sommelier answered 2/9, 2013 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.