Java client library to use the Watson APIs.
Table of Contents
- You need an IBM Cloud account.
All the services:
<dependency>
<groupId>com.ibm.watson</groupId>
<artifactId>ibm-watson</artifactId>
<version>8.4.0</version>
</dependency>Only Discovery:
<dependency>
<groupId>com.ibm.watson</groupId>
<artifactId>discovery</artifactId>
<version>8.4.0</version>
</dependency>All the services:
'com.ibm.watson:ibm-watson:8.4.0'Only Assistant:
'com.ibm.watson:assistant:8.4.0'Download the jar with dependencies here.
Now, you are ready to see some examples.
The examples within each service assume that you already have service credentials. If not, you will have to create a service in IBM Cloud.
If you are running your application in IBM Cloud (or other platforms based on Cloud Foundry), you don't need to specify the
credentials; the library will get them for you by looking at the VCAP_SERVICES environment variable.
When running in IBM Cloud (or other platforms based on Cloud Foundry), the library will automatically get the credentials from VCAP_SERVICES.
If you have more than one plan, you can use CredentialUtils to get the service credentials for an specific plan.
Watson services are migrating to token-based Identity and Access Management (IAM) authentication.
- With some service instances, you authenticate to the API by using IAM.
- In other instances, you authenticate by providing the username and password for the service instance.
- If you're using a Watson service on Cloud Pak for Data, you'll need to authenticate in a specific way.
To find out which authentication to use, view the service credentials. You find the service credentials for authentication the same way for all Watson services:
- Go to the IBM Cloud Dashboard page.
- Either click an existing Watson service instance in your resource list or click Create resource > AI and create a service instance.
- Click on the Manage item in the left nav bar of your service instance.
On this page, you should be able to see your credentials for accessing your service instance.
In your code, you can use these values in the service constructor or with a method call after instantiating your service.
There are two ways to supply the credentials you found above to the SDK for authentication.
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.
The file downloaded will be called ibm-credentials.env. This is the name the SDK will search for and must be preserved unless you want to configure the file path (more on that later). The SDK will look for your ibm-credentials.env file in the following places (in order):
- Your system's home directory
- The top-level directory of the project you're using the SDK in
As long as you set that up correctly, you don't have to worry about setting any authentication options in your code. So, for example, if you created and downloaded the credential file for your Discovery instance, you just need to do the following:
Discovery service = new Discovery("2019-04-30");And that's it!
If you're using more than one service at a time in your code and get two different ibm-credentials.env files, just put the contents together in one ibm-credentials.env file and the SDK will handle assigning credentials to their appropriate services.
If you would like to configure the location/name of your credential file, you can set an environment variable called IBM_CREDENTIALS_FILE. This will take precedence over the locations specified above. Here's how you can do that:
export IBM_CREDENTIALS_FILE="<path>"where <path> is something like /home/user/Downloads/<file_name>.env.
If you'd prefer to set authentication values manually in your code, the SDK supports that as well. The way you'll do this depends on what type of credentials your service instance gives you.
Some services use token-based Identity and Access Management (IAM) authentication. IAM authentication uses a service API key to get an access token that is passed with the call. Access tokens are valid for approximately one hour and must be regenerated.
You supply either an IAM service API key or an access token:
- Use the API key to have the SDK manage the lifecycle of the access token. The SDK requests an access token, ensures that the access token is valid, and refreshes it if necessary.
- Use the access token if you want to manage the lifecycle yourself. For details, see Authenticating with IAM tokens.
Supplying the IAM API key:
// letting the SDK manage the IAM token
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
Discovery service = new Discovery("2019-04-30", authenticator);Supplying the access token:
// assuming control of managing IAM token
Authenticator authenticator = new BearerTokenAuthenticator("<access_token>");
Discovery service = new Discovery("2019-04-30", authenticator);Authenticator authenticator = new BasicAuthenticator("<username>", "<password>");
Discovery service = new Discovery("2019-04-30", authenticator);Authenticating with ICP is similar to the basic username and password method, except that you need to make sure to disable SSL verification to authenticate properly. See here for more information.
Authenticator authenticator = new BasicAuthenticator("<username>", "<password>");
Discovery service = new Discovery("2019-04-30", authenticator);
HttpConfigOptions options = new HttpConfigOptions.Builder()
.disableSslVerification(true)
.build();
service.configureClient(options);Like IAM, you can pass in credentials to let the SDK manage an access token for you or directly supply an access token to do it yourself.
// letting the SDK manage the token
Authenticator authenticator = new CloudPakForDataAuthenticator(
"<CP4D token exchange base URL>",
"<username>",
"<password>",
true, // disabling SSL verification
null,
);
Discovery service = new Discovery("2019-04-30", authenticator);
service.setServiceUrl("<service CP4D URL>");// assuming control of managing the access token
Authenticator authenticator = new BearerTokenAuthenticator("<access_token>");
Discovery service = new Discovery("2019-04-30", authenticator);
service.setServiceUrl("<service CP4D URL>");