node.js http和bingsearchapi

我正尝试使用必应searchAPI来返回一个JSONstring。 我第一次尝试使用以下url按照Azure的探索网站( https://datamarket.azure.com/dataset/explore/5BA839F1-12CE-4CCE-BF57-A49D98D29A44 ):

'https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27NGI%20SPA%27&Market=%27en-US%27' 

之后,我发现了一个SO线程使用新的Bing API(nodejs) ,它build议我使用表单的url:

 https://user:<YourDefaultAccountKey>@api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27leo%20fender%27&Market=%27en-US%27&$top=50&$format=JSON 

这两个返回状态401(authentication失败):

 STATUS: 401 HEADERS: {"content-type":"application/json; charset=utf-8","server":"Microsoft-IIS/8.0","jsonerror":"true","x-powered-by":"ASP.NET","access-control-allow-origin":"*","access-control-allow-credentials":"false","access-control-allow-headers":"Authorization, DataServiceVersion, MaxDataServiceVersion","access-control-expose-headers":"DataServiceVersion, MaxDataServiceVersion","access-control-allow-methods":"GET, POST, OPTIONS","access-control-max-age":"604800","date":"Wed, 02 Jul 2014 17:23:29 GMT","content-length":"91"} BODY: {"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""} 

我也尝试了其他各种url的组合,无济于事。 我的代码如下:

 var url = require('url'); var http = require('http'); var serviceRootURL = 'https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27NGI%20SPA%27&Market=%27en-US%27' var params = 'hi'; var dataURL = url.parse(serviceRootURL); var post_options = { hostname: dataURL.hostname, port: dataURL.port || 80, path: dataURL.path, method: 'GET', headers: { 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': params.length } }; var req = http.request(post_options, function(res) { console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); // write data to request body req.write('data\n'); req.write('data\n'); req.end(); 

任何想法,为什么我得到authentication失败?

你可以使用这个封装请求的模块 ,所以你可以像这样使用它:

 var Bing = require('node-bing-api')({ accKey: "your-account-key" }); Bing.web("leo fender", function(error, res, body){ console.log(body); }, { top: 50, market: 'en-US' }); 

它适用于Azure版本。 您只需要更换您的帐户密钥。

得到它与请求…怪异的工作

 var request = require('request'); var _ = require('underscore'); var searchURL = 'https://user:<TIPE YOUR KEE HEER>@api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query=%27xbox%27&$top=10&$format=JSON'; var http = request( searchURL, function(err, resp, body) { if ( err ) { throw err; } var a = JSON.parse(body); console.log(adresults); }); 

你可以使用jsearch模块。 安装

 npm install jsearch 

用法;

 js.bing('queryStringYouWant',10,function(response){ console.log(response) // for Bing results })