使用node.js在我的站点中实现yahoo搜索时的Yahoo OAuth授权问题

时间:2014-08-21 07:14:40

标签: javascript node.js oauth yahoo yahoo-api

我尝试使用OAuth进行雅虎搜索。我是这个概念的新手。

我在雅虎注册并获得了消费者和密钥。

当我使用JavaScript测试我的代码(test.js)时,我使用node.js实现了它(我安装了 npm install oauth -g )我得到以下error而不是XML。

{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }

TEST.JS

var OAuth = require('oauth').OAuth;
var key = 'dj0yJmk9cU5GT2p5T0VRc3R2JmQ9WVdrOVZHeFZaR1YwTjJFbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1lYg--';
var secret = 'e766c8cbd1c7b31612b1787e1f39b7b27a88433d';
var webSearchUrl = 'https://ysp.yahooapis.com/ysp/web';
var finalUrl = webSearchUrl + '?q=iphone';
var oa = new OAuth(webSearchUrl, webSearchUrl, key, secret, "1.0", null, "HMAC-SHA1", null, {"Accept-Language": null, "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0", "Cookie" : ""});
oa.setClientOptions({ requestTokenHttpMethod: 'GET' });
oa.get(finalUrl, '','', function (error, data, resp) {
    if (error || resp.statusCode !== 200) { // if there is a error, exit with error message
        return console.log(error);
    } 
    // otherwise, echo incoming data
    console.log(data);
});

enter image description here

请帮助我获取XML数据进行处理。提前感谢您的帮助。

更新问题

在test.js中michaelchum给出的链接中尝试实现后,我发出以下错误PFA SS和test2.js代码。

Test2.js

var YaBoss = require('yaboss');
var YaBossClient = new YaBoss('someCustomerKey', 'someCustomerSecret');

YaBossClient.search('web','yahoo', {count: 10}, function(err,dataFound,response){
    console.log("Data :*****");
    console.log(dataFound);
    console.log("Error :*****");
    console.log(err);
});

enter image description here

1 个答案:

答案 0 :(得分:1)

这是因为Yahoo Search BOSS API需要 OAuth1.0A ,您已在OAuth库调用中选择了OAuth1.0。

实际上有一个Yahoo Search API npm package,您应该看一下,它会使实施变得更加容易。

试试这个:

制作新文件夹和新文件app.js

var YaBoss = require('yaboss');

var key = 'dj0yJmk9cU5GT2p5T0VRc3R2JmQ9WVdrOVZHeFZaR1YwTjJFbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1lYg--';
var secret = 'e766c8cbd1c7b31612b1787e1f39b7b27a88433d';

var YaBossClient = new YaBoss(key, secret);

YaBossClient.search('web','yahoo', {count: 10}, function(err,dataFound,response){
    console.log("Data :*****");
    console.log(dataFound);
    console.log("Error :*****");
    console.log(err);
});

在文件夹中执行npm install yaboss,然后node app.js