Search code examples
node.jsibm-cloudibm-watsonpersonality-insights

Calling Watson Personality Insights from localhost


I have already downloaded Personality Insights from Bluemix,and I can run it on my command line successfully, but how can i insert it on my webpage and run it locally?

Does anyone have sample code for index.js? I have set up the server but I am finding it difficult to integrate it on my page.

var express = require('express');
var app = express();
var http = require('http').Server(app);
var cfenv = require("cfenv");

var appEnv = cfenv.getAppEnv();

http.listen(appEnv.port, appEnv.bind);

var PersonalityInsightsV2 = require('watson-developer-cloud/personality-insights/v2');

var personality_insights = new PersonalityInsightsV2({
  username: '<YOUR-USERNAME>',
  password: '<YOUR-PASSWORD>'
});

personality_insights.profile({
  text: "<YOUR-100-UNIQUE-WORDS>",
  language: 'en' },
  function (err, response) {
    if (err)
      console.log('error:', err);
    else
      console.log(JSON.stringify(response, null, 2));
});    

Solution

  • This is not recommended because in theory you could directly make the call from the web page (browser side java script) to the Watson Personality Insights REST service but then you are giving away the credentials.

    So the recommended way to do it is using an intermediate http proxy/gateway style component (which can be implemented in Node.js as well).

    So basically your browser based java script will invoke your own REST service on IBM Bluemix (e.g. a Node.js application, but can also use OpenWhisk, NodeRED, JAVA, ...) and the call to the Watson Service is done from there, so your credentials are save (either hard code it, or use VCAP_SERVICES)

    Edit: 7.9.16: In case you really want to do it locally just use a local node.js instance for doing it and you can obtain the service credentials of the Watson service by logging in into the Bluemix web interface. It is described here

    The UI slightly changed, so you have to click on the top left button, choose Watson->Personality Insights->Service Credentials->View Credentials (on the Credentials-1 entry)