React JS - IBM Watson Personality InsightsV3 Token Auth - CORS error

212 Views Asked by At

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) };
1

There are 1 best solutions below

1
On

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.