Search code examples
node.jsoauthtumblr

Node.js request module with tumblr oauth: oauth signature doesn't match


I am writing a web application which fetches content from various networks like twitter, tumblr, etc. Have a look at the following code:

User.prototype.tumblr_authenticate_url = function(callback) {
var oauth = {
    consumer_key: config.tumblr.tumblr_consumer_key,
    consumer_secret: config.tumblr.tumblr_consumer_secret
};
request.post({url:'http://www.tumblr.com/oauth/request_token', oauth:oauth}, function (e, r, body) {            
    if(e)
        return callback(e);
    var access_token = qs.parse(body);

    if(!access_token || typeof(access_token.oauth_token) == 'undefined' || typeof(access_token.oauth_token_secret) == 'undefined')
        return callback(1);
    return callback(null,{
        url : 'http://www.tumblr.com/oauth/authorize?oauth_token=' + access_token.oauth_token,
        oauth_token : access_token.oauth_token,
        oauth_token_secret : access_token.oauth_token_secret});
});
};

User.prototype.tumblr_access_token = function(token,verifier,callback) {
var _oauth = {
    consumer_key: config.tumblr.tumblr_consumer_key,
    consumer_secret: config.tumblr.tumblr_consumer_secret
};
_oauth.token = token;
_oauth.verifier = verifier;
request.post({url:'http://www.tumblr.com/oauth/access_token', oauth:_oauth}, function (e, r, body) {
    if(e)
        return callback(e);
    var user_details = qs.parse(body);
    console.log(body);
    if(!user_details || typeof(user_details.oauth_token)=='undefined' || typeof(user_details.oauth_token_secret)=='undefined' )
        return callback(1);

    return callback(null,{
        oauth_token : user_details.oauth_token,
        oauth_token_secret : user_details.oauth_token_secret});
});
};

Now, the tumblr_authenticate url part works perfectly. It fetches the request tokens successfully, and I get a proper authentication page. However, when I copy the parameters from the redirected url and try them with the tumblr_access_token function, I get

oauth_signature [xxxx] does not match expected value [yyy]

in the response body.
I am using the exact same code for twitter, and this works perfectly. I am wondering if I am doing anything wrong with the code, or if tumblr oauth works slightly differently and it accepts parameters in a different way. From what I could find out, twitter and tumblr use the same signature algorithm HMAC-SHA1.


Solution

  • I ended up using the passport and passport-tumblr npm module to solve this issue. I think tumblr may use a slightly different approach to oauth, so the same code may not work.