我创建了一个简单的测试程序。我只是想为Watson Discovery服务器ping Env详细信息(https://www.ibm.com/watson/developercloud/discovery/api/v1/?node#list_environment_details)。
const dotenv = require( 'dotenv');
dotenv.config()
const DiscoveryV1 = require('watson-developer-cloud/discovery/v1');
var discovery = new DiscoveryV1({
username: process.env.USERNAME,
password: process.env.PASSWORD,
version_date: '2017-11-07'
});
discovery.getEnvironments({}, function(error, data) {
console.log('Env:', JSON.stringify(data, null, 2));//returns data
//doesn't appear to be called
discovery.getEnvironment({ environment_id: process.env.ENVIRONMENT}), function(error, data) {
if (error){
console.log('Env Details', JSON.stringify(error, null, 2));
}
console.log('Env Details', JSON.stringify(data, null, 2));
};
});
结果 node index.js
Env: {
"environments": [
{
"environment_id": "system",
"name": "Watson System Environment",
"description": "Shared system data sources",
"read_only": true
},
{
"environment_id": "my value",
"name": "byod",
"description": "",
"created": "2018-01-24T14:52:47.736Z",
"updated": "2018-01-24T14:52:47.736Z",
"read_only": false
}
]
}
我可以使用curl命令来使用它,所以我知道我有正确的信息。它只是节点调用。
curl -u "{username}":"{pw}" "https://gateway.watsonplatform.net/discovery/api/v1/environments/my value?version=2017-11-07"
答案 0 :(得分:0)
我能让它工作的唯一方法是在节点中使用curl。
var options = {
url: 'https://gateway.watsonplatform.net/discovery/api/v1/',
auth: {
'user': process.env.WATSON_DISCOVERY_USERNAME,
'pass': process.env.WATSON_DISCOVERY_PASSWORD
}
};
request(options, function(error, response, body){
if (!error && response.statusCode == 200) {
//console.log('from made callback', body);
res.json({
"input" : req.body.input.text,
response : body});
}
});