Investigated how to call services with Watson SDK for Java

This is the original story

https://github.com/watson-developer-cloud/java-sdk

What's wrong

At first glance, I was guided to this code part that says "Yes, yes, I'm passing the API_Key" ...

Sample.java


// letting the SDK manage the IAM token
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
Discovery service = new Discovery("2019-04-30", authenticator);

basic way of thinking

If you deploy to IBM Cloud, you can use VCAP variables, so you can create an instance of the service without specifying Authenticator. (Those who think about difficult things lose)

When doing unit tests

It's easy to fetch the Credential File and load it (there's no VCAP variable, but it feels like it's there)

Credential file (easier!)

With a credential file, you just need to put the file in the right place and the SDK will do the work of parsing it and authenticating. You can get this file by clicking the Download button for the credentials in the Manage tab of your service instance.

I was also struck by the fact that the Credentials pane was sometimes not displayed on this page ("Administration" under "Services"). (I wonder if it failed to read ...?) nlc.png

With this Download, you can drop ʻibm-credentials.env. The format is _ = `

This NLC service was created in the Tokyo region that I found, so be careful that the URL is for the Tokyo region. When I tried and errored before loading the file, I was able to pass the API-Key but I could not specify the URL, in which case https://gateway.watsonplatform.net/natural-language-classifier/api Is specified, and 403 Forbidden is returned when trying to access ʻUS-South`.

Specify API as an argument If you created the service in a region other than && US-South, set the URL.

Sample.java


NaturalLanguageClassifier nlc = new NaturalLanguageClassifier("<api_key");
//Specify the URL of the service region
nlc.setURL("https://gateway-tok.watsonplatform.net/natural-language-classifier/api");

Where and in order to load ibm-credentials.env

I wrote it in the Readme, but it was easier to understand if I looked at the source.

File name fixed at ʻibm-credentials.env` The reading order is as follows, and the one found from the top is read (do not read after that)

CredentialUtils.java


  private static List<File> getFilesToCheck() {
    List<File> files = new ArrayList<>();

    String userSpecifiedPath = EnvironmentUtils.getenv("IBM_CREDENTIALS_FILE");
    String currentWorkingDirectory = System.getProperty("user.dir");
    String unixHomeDirectory = EnvironmentUtils.getenv("HOME");
    String windowsFirstHomeDirectory = EnvironmentUtils.getenv("HOMEDRIVE") + EnvironmentUtils.getenv("HOMEPATH");
    String windowsSecondHomeDirectory = EnvironmentUtils.getenv("USERPROFILE");

https://github.com/IBM/java-sdk-core/blob/master/src/main/java/com/ibm/cloud/sdk/core/util/CredentialUtils.java

Also note the version of Apache Commons IO

I used Commons for another purpose, and the version that came in was a bit old, but for the time being, it seems to be useless unless it is 2.3 or higher (Commons IO specifies 2.6 in the pom of SDK Core) This method does not exist and an error (NoSuchMethodError) occurs. https://commons.apache.org/proper/commons-io/javadocs/api-2.6/org/apache/commons/io/IOUtils.html#readLines-java.io.InputStream-java.nio.charset.Charset

Recommended Posts

Investigated how to call services with Watson SDK for Java
[Java] How to test for null with JUnit
How to call functions in bulk with Java reflection
How to call with One Touch (without confirmation) on Android (Java)
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
Call Visual Recognition in Watson Java SDK
Let's go with Watson Assistant (formerly Conversation) ④ How to link with Java logic
How to use Java framework with AWS Lambda! ??
How to use Java API with lambda expression
[Java] (for MacOS) How to set the classpath
How to use nginx-ingress-controller with Docker for Mac
[Java] How to make multiple for loops single
How to call AmazonSQSAsync
How to build docker environment with Gradle for intelliJ
[Java] How to omit spring constructor injection with Lombok
How to deploy Java to AWS Lambda with Serverless Framework
How to deal with No template for interactive request
[For beginners] How to operate Stream API after Java 8
How to use Truth (assertion library for Java / Android)
Try connecting to AzureCosmosDB Emulator for Docker with Java
How to build Java development environment with VS Code
How to make Laravel faster with Docker for Mac
How to loop Java Map (for Each / extended for statement)
How to execute WebCamCapture sample of NyARToolkit for Java
[Java] How to start a new line with StringBuilder
(Java) How to implement equals () for a class with value elements added by inheritance
How to lower java version
[Java] How to use Map
How to decompile apk file to java source code with MAC
How to use SAS tokens for Azure Event hubs (Java)
Java to play with Function
Java --How to make JTable
[Must-see for apprentice java engineer] How to use Stream API
How to create a lightweight container image for Java apps
How to handle exceptions coolly with Java 8 Stream or Optional
How to use java Optional
How to minimize Java images
How to write java comments
How to use java class
[Java] How to display Wingdings
[Java] How to use string.format
How to number (number) with html.erb
How to use Java Map
How to call and use API in Java (Spring Boot)
How to test a private method with RSpec for yourself
How to update with activerecord-import
How to set Java constants
Connect to DB with Java
Connect to MySQL 8 with Java
How to use Java variables
How to convert Java radix
Try Spark Submit to EMR using AWS SDK for Java
[Java] How to use Optional ①
For Java beginners: List, Map, Iterator / Array ... How to convert?
How to initialize Java array
[Java] How to search for a value in an array (or list) with the contains method
How to deal with the type that I thought about writing a Java program for 2 years
Encrypt data uploaded to S3 using AWS SDK for Java / SSE-KMS
Android: How to deal with "Could not determine java version from '10 .0.1'"
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java