1
votes

I'm trying to connect my Java application to the Watson NLU service. For a start, I tried to follow the tutorial from Bluemix. I created a service on Bluemix and imported the watson Java SDK. Using this tutorial code, I keep receiving 401 - not authorized responses. (Of course i changed username and password for the service).

I guess there's something missing, but i can't figure out what.

NaturalLanguageUnderstanding service = new NaturalLanguageUnderstanding(
  NaturalLanguageUnderstanding.VERSION_DATE_2017_02_27,
  "{username}",
  "{password}"
);

String text = "IBM is an American multinational technology " +
  "company headquartered in Armonk, New York, " +
  "United States, with operations in over 170 countries.";

EntitiesOptions entitiesOptions = new EntitiesOptions.Builder()
  .emotion(true)
  .sentiment(true)
  .limit(2)
  .build();

KeywordsOptions keywordsOptions = new KeywordsOptions.Builder()
  .emotion(true)
  .sentiment(true)
  .limit(2)
  .build();

Features features = new Features.Builder()
  .entities(entitiesOptions)
  .keywords(keywordsOptions)
  .build();

AnalyzeOptions parameters = new AnalyzeOptions.Builder()
  .text(text)
  .features(features)
  .build();

AnalysisResults response = service
  .analyze(parameters)
  .execute();

System.out.println(response);
2

2 Answers

1
votes

i had the same problems in node.js and solved it by adding the correct url of the api gateway to the NaturalLanguageUnderstanding service = new NaturalLanguageUnderstanding() object. please keep in mind, that this depends on your region ..

regards Leo

0
votes

A 401 Unauthorized would suggest that there's an issue with the credentials that you're using to access the service. To rule this out, take a start by using the cURL tutorial from that same page:

curl -X POST \
-H "Content-Type: application/json" \
-u "{username}":"{password}" \
-d @parameters.json "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27"

If, with that same username and password, you still receive a 401 Unauthorized error, then there's likely an issue with that username/password combination. Delete the tile in Bluemix and create a new one to get a new username/password, and give that a try.

If that does work fine, then there's an issue with how the username/password is being inserted into the code. Verify that you've replaced {username} and {password}, the final version should not have any curly brackets in it.