How to send the OAuth request in Node

18,332

We use https://github.com/ciaranj/node-oauth

Share:
18,332
Jeffrey
Author by

Jeffrey

Sr. Architect at Juranzhijia. Has strong experience in designing the architecture of cloud products and devops system. Frequently used languages are C++, Node.js, javascript. Develop on Windows, Mac OS X and Ubuntu platforms. Interested in programming for the embedded smart devices.

Updated on June 18, 2022

Comments

  • Jeffrey
    Jeffrey almost 2 years

    I want to access the WS REST API in node.js. I have the oauth_consumer_key and the oauth_token and the API end point. The oauth_signature_method is HMAC-SHA1.

    How to send the OAuth request in Node?

    Is there a module/library to generate the request headers? What I expect is a function like:

    var httprequest = createRequest(url, method, consumer_key, token);
    

    • UPDATE 10/14/2012. Adding the solution.

    I'm using the code below.

    var OAuth = require('oauth').OAuth;
    
    consumer = new OAuth('http://term.ie/oauth/example/request_token.php',
                        'http://term.ie/oauth/example/access_token.php',
                        'key', 'secret', '1.0',
                        null, 'HMAC-SHA1');
    
    // Get the request token                    
    consumer.getOAuthRequestToken(function(err, oauth_token, oauth_token_secret, results ){
        console.log('==>Get the request token');
        console.log(arguments);
    });
    
    
    // Get the authorized access_token with the un-authorized one.
    consumer.getOAuthAccessToken('requestkey', 'requestsecret', function (err, oauth_token, oauth_token_secret, results){
        console.log('==>Get the access token');
        console.log(arguments);
    });
    
    // Access the protected resource with access token
    var url='http://term.ie/oauth/example/echo_api.php?method=foo&bar=baz';
    consumer.get(url,'accesskey', 'accesssecret', function (err, data, response){
        console.log('==>Access the protected resource with access token');
        console.log(err);
        console.log(data);
    });