2

我正在使用 IBM Watson 的检索和排名服务。该服务提供返回搜索结果的 REST API。以下是 API URL

https://username:password@gateway.watsonplatform.net/retrieve-and-rank/api/v1/solr_clusters/sc6e46e4f5_f30c_4a8a_ae9c_b4ab6914f7b4/solr/example-collection/select?q=some question&wt=json&fl=id,title,body

如您所见,此 URL 包含用户名和密码。Retreive and Rank 文档提到了调用 API 的上述模式,即使用用户名和密码作为 URL 的一部分。如果我将其粘贴到谷歌浏览器中,它会出现再次输入用户名和密码的对话框。输入凭据后,我可以看到数据。

我的问题是,如何通过 Node.js 调用这样的 URL。我不知道从哪里开始以及应该遵循哪些步骤。

4

2 回答 2

3

IBM Watson 的 Retrieve and Rank 服务的 API 使用基本身份验证。方法有多种,其中之一 - 使用模块request

var url = "https://gateway.watsonplatform.net/retrieve-and-rank/api/v1/solr_clusters/sc1ca23733_faa8_49ce_b3b6_dc3e193264c6/solr/example_collection/select?q=what%20is%20the%20basic%20mechanism%20of%20the%20transonic%20aileron%20buzz&wt=json&fl=id,title"

request.get('http://some.server.com/', {
  auth: {
    user: 'username',
    pass: 'password',
    sendImmediately: false
  },
  json: true
}, function(error, response, body) {
     console.log( 'Found: ', body.response.numFound );
});

或者

var username = 'username',
    password = 'password',
    url = "https://" + user + ":" + password + "@" + "gateway.watsonplatform.net/retrieve-and-rank/api/v1/solr_clusters/sc1ca23733_faa8_49ce_b3b6_dc3e193264c6/solr/example_collection/select?q=what%20is%20the%20basic%20mechanism%20of%20the%20transonic%20aileron%20buzz&wt=json&fl=id,title"

request({url: url, json: true}, function (error, response, body) {
   console.log( 'Found: ', body.response.numFound );
});
于 2016-02-24T11:30:56.473 回答
1

IBM Watson 有一个您可能希望在应用程序中使用的节点 SDK: https ://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/retrieve-and-rank/api/v1/?node#

var watson = require('watson-developer-cloud');
var retrieve_and_rank = watson.retrieve_and_rank({
  username: '{username}',
  password: '{password}',
  version: 'v1'
});
于 2016-02-25T07:50:02.013 回答