我一直在尝试将watson shitalalitySightsv3 api实现到我的react JS应用中。为了避免使用凭据时出现的CORS错误,我有一个烧瓶/python服务器,请求令牌。然后,我在React JS文件中使用获取来接收令牌,并尝试将其传递给我的Watson个人资料请求。尽管如此,我仍然遇到CORS错误,不确定如何使用令牌完成请求。
try { axios.get('http://project1-m6.c9users.io:8080/token')
.then(result => { console.log('AAA ' + result.data)
var PersonalityInsightsV3 = require('watson-developer-cloud/personality-insights/v3');
var personality_insights = new PersonalityInsightsV3({
username: 'myname',
password: 'mypassword',
version_date: '2017-10-13',
headers: { 'X-Watson-Authorization-Token': result.data }
});
personality_insights.profile(
{
content: input,
content_type: 'text/plain',
consumption_preferences: true,
raw_scores: true
},
function(err, response) {
if (err) {
console.log('error:', err);
} else {
console.log(JSON.stringify(response, null, 2));
_this.props.history.push(
{pathname: '/personality', state: {result:JSON.stringify(response, null, 3)}})
}
}
);
}
);
} catch(e) { console.log(e) };
根据文档可能是不可能的:https://www.npmjs.com/package/watson-developer-cloud#clount-side-usage
某些端点/服务不支持CORS,因此您可能必须在Python应用中进行更多代理。
我不知道PY中流行的软件包是什么,但是Checkout Express-HTTP-Proxy。