oauth有npm吗?

时间:2015-10-30 14:28:23

标签: javascript node.js oauth oauth-2.0

我想从我的节点服务器向yelp.com提出请求,但我需要在我的请求中使用OAuth.getParameterMap和其他OAuth函数。我想知道是否有任何关于我需要的功能的npm?

到目前为止,这是我的代码:

app.post('/yelpApp', function (req, res)
{
    var auth = {
    consumerKey: conKey,
    consumerSecret: conSec,
    accessToken: accTok,
    accessTokenSecret: accTokSec,
    serviceProvider: {
      signatureMethod: "HMAC-SHA1"
      }
    };

    var terms = 'food';
    var near = 'London';
    var accessor = {
      consumerSecret: auth.consumerSecret,
      tokenSecret: auth.accessTokenSecret
    };
    parameters = [];
    parameters.push(['term', terms]);
    parameters.push(['location', near]);
    parameters.push(['callback', 'cb']);
    parameters.push(['oauth_consumer_key', auth.consumerKey]);
    parameters.push(['oauth_consumer_secret', auth.consumerSecret]);
    parameters.push(['oauth_token', auth.accessToken]);
    parameters.push(['oauth_signature_method', 'HMAC-SHA1']);

    var message = {
      'action': 'http://api.yelp.com/v2/search',
      'method': 'GET',
      'parameters': parameters
    };

    //can't use these OAuth functions on server
    OAuth.setTimestampAndNonce(message);
    OAuth.SignatureMethod.sign(message, accessor);
    var parameterMap = OAuth.getParameterMap(message.parameters);
    parameterMap.oauth_signature =  OAuth.percentEncode(parameterMap.oauth_signature)

    $.ajax(
    {
    'url': message.action,
    'data': parameterMap,
    'cache': true,
    'dataType': 'json',
    'success': function(data, textStats, XMLHttpRequest) 
        {
            res.send(JSON.stringify({foo: data}));
        }
     });
});

2 个答案:

答案 0 :(得分:1)

Passport处理此问题。看看吧;)

答案 1 :(得分:0)

Node-yelp提供了与Yelp接口的方法,包括身份验证。这取决于oauth