Call IBM Watson APIs to add Machine Learning to your App

You can easily add very cool IBM Watson APIs to your application. You need to sign up for Bluemix, create an application and bind APIs or services to the app. This will create credentials (username and password) for each API or service.

Create a Bluemix acccount

First if you have not signed up for a Bluemix account yet, go to http://ibm.biz/bluemixnyc and Sign Up.

Create an App

Once you have confirmed your account, go to the Catalog and choose any Runtime. We will only use the runtime to bind services to, which will give us API credentials. For example, name the app ‘my-app1’.

Add a Service

Now, to add Services either go to Catalog > Services, and add a service to your app, or go to your app’s admin page. Bluemix is based on Cloud Foundry, so apps in Bluemix are listed under ‘CF Apps.’ Go to Dashboard > CF Apps > ‘my-app1’, and click the ‘Add a Service or API.’ I will add Personality Insights to my app in this example. Edit the required fields and click the Create button. Bluemix might ask you to restage your app. Now we’re ready to call the Personality Insights API.

Call the Watson API

To make a REST call to a Watson API, you must add an Authorization header with a Base64 encoded “Basic :” string. To generate the Basic Authorization header, use the colon as a separator for username and password and Base64 encode the Authorization string.

String username = "username from your service credentials";
String password = "password from your service credentials";
String s = username + ":" + password;
byte[] b = s.getBytes();
String enc = Base64.encodeBase64String(b);
String authorization = "Basic "+enc;

To call the API with the above credentials use the following curl statement from command line.

curl -X POST -H "Authorization: Basic BzAvEATz4UYqKfU5JA66GSIwKNgdATGiADR8YiWaOSH2Purd6tedhKUJaPa3KLshLq==" -H "Content-Type: text/plain" -H "Accept: application/json" -H "Accept-Language: en" -H "headers: true" -d 'put your text here' https://gateway.watsonplatform.net/personality-insights/api/v2/profile

For the API reference details go here
http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/#!/personality-insights/profile

This curl command will return a response object that you can visualize with the Visualize Personality Insights API.
http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/#!/personality-insights/visualize

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.