https://github.com/watson-developer-cloud/java-sdk
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);
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)
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 ...?)
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");
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
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