如何在Node中发送OAuth请求

Jef*_*rey 7 module oauth node.js

我想访问node.js中的WS REST API.我有oauth_consumer_keyoauth_tokenAPI端点.oauth_signature_method是HMAC-SHA1.

如何在Node中发送OAuth请求?

是否有模块/库来生成请求标头?我期望的功能如下:

var httprequest = createRequest(url, method, consumer_key, token);
Run Code Online (Sandbox Code Playgroud)
  • 更新10/14/2012.添加解决方案.

我正在使用下面的代码.

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);
});
Run Code Online (Sandbox Code Playgroud)

sko*_*yov 9

我们使用https://github.com/ciaranj/node-oauth

  • 这个npm模块肯定需要更多的文档或至少一些关于每件作品的注释/评论.我知道OAuth是一种标准,但不同的提供商需要/期望不同的东西,具体取决于提供商是谁以及您想要访问哪些服务.他与这些例子的联系也已经死了.对于那些不确切知道在OAuth中做什么的人来说非常令人沮丧和失望. (11认同)