0

I have been attempting to implement the Watson PersonalityInsightsV3 API into my React JS app. In order to circumvent the CORS error that arises when using credentials I have a flask/python server making a request for the token. I then use fetch inside my React JS files to receive the token and attempt to pass it to my Watson profile request. I am still getting the CORS error despite this and am not sure how to complete the request with the token.

 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) };
Charles
  • 11,269
  • 13
  • 67
  • 105

1 Answers1

0

This may not be possible according to the docs: https://www.npmjs.com/package/watson-developer-cloud#client-side-usage

Some endpoints/services don't support cors, so you may have to do some more proxying in your Python app.

I don't know what the popular packages are for that in Py, but checkout express-http-proxy.

Jon Jaques
  • 4,262
  • 2
  • 23
  • 25
  • The Watson Devloper Cloud Git shows that PersonalityInsights does support CORS so that isn't the issue. https://github.com/watson-developer-cloud/node-sdk/tree/master/examples/webpack – Nicholas Herrera Feb 12 '18 at 13:31