Skip to content

Commit 6f80223

Browse files
committed
docs(main README): Add documentation about using IAM
1 parent 66773bd commit 6f80223

File tree

1 file changed

+82
-5
lines changed

1 file changed

+82
-5
lines changed

README.md

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ Java client library to use the [Watson APIs][wdc].
1515
* [Gradle](#gradle)
1616
* [Usage](#usage)
1717
* [Running in IBM Cloud](#running-in-ibm-cloud)
18-
* [Getting the Service Credentials](#getting-the-service-credentials)
18+
* [Authentication](#authentication)
19+
* [Username and Password](#username-and-password)
20+
* [API Key](#api-key)
21+
* [Using IAM](#using-iam)
1922
* IBM Watson Services
2023
* [Assistant](assistant)
2124
* [Discovery](discovery)
@@ -123,17 +126,91 @@ credentials; the library will get them for you by looking at the [`VCAP_SERVICES
123126
When running in IBM Cloud (or other platforms based on Cloud Foundry), the library will automatically get the credentials from [`VCAP_SERVICES`][vcap_services].
124127
If you have more than one plan, you can use `CredentialUtils` to get the service credentials for an specific plan.
125128

126-
## Getting the Service Credentials
129+
## Authentication
127130

128-
You will need the `username` and `password` (`api_key` for Visual Recognition) credentials, and the API endpoint for each service. Service credentials are different from your IBM Cloud account username and password.
131+
There are three ways to authenticate with IBM Cloud through the SDK: using a `username` and `password`, using an `api_key`, and with IAM.
129132

130-
To get your service credentials, follow these steps:
133+
Getting the credentials necessary for authentication is the same process for all methods. To get them, follow these steps:
131134

132135
1. Log in to [IBM Cloud](https://console.bluemix.net/catalog?category=watson)
133136
1. In the IBM Cloud **Catalog**, select the service you want to use.
134137
1. Click **Create**.
135138
1. On the left side of the page, click **Service Credentials**, and then **View credentials** to view your service credentials.
136-
1. Copy `url`, `username` and `password`(`api_key` for AlchemyAPI or Visual Recognition).
139+
1. Copy the necessary credentials (`url`, `username`, `password`, `api_key`, `apikey`, etc.).
140+
141+
In your code, you can use these values in the service constructor or with a method call after instantiating your service. Here are some examples:
142+
143+
### Username and Password
144+
145+
```java
146+
// in the constructor
147+
Discovery service = new Discovery("2017-11-07", "<username>", "<password>");
148+
```
149+
150+
```java
151+
// after instantiation
152+
Discovery service = new Discovery("2017-11-07");
153+
service.setUsernameAndPassword("<username>", "<password>");
154+
```
155+
156+
### API Key
157+
158+
_Note: This version of instantiation only works with Visual Recognition, as it's the only service that uses an API key rather than a username and password._
159+
160+
```java
161+
// in the constructor
162+
VisualRecognition service = new VisualRecognition("2016-05-20", "<api_key>");
163+
```
164+
165+
```java
166+
// after instantiation
167+
VisualRecognition service = new VisualRecognition("2016-05-20");
168+
service.setApiKey("<api_key>");
169+
```
170+
171+
### Using IAM
172+
173+
When authenticating with IAM, you have the option of passing in the IAM API key, the IAM URL, and an IAM access token. **Be aware that passing in an access token means that you're assuming responsibility for maintaining that token's lifecycle.** If you instead pass in an IAM API key, the SDK will manage it for you.
174+
175+
```java
176+
// in the constructor, letting the SDK manage the IAM token
177+
IamOptions options = new IamOptions.Builder()
178+
.apiKey("<iam_api_key>")
179+
.url("<iam_url>")
180+
.build();
181+
Discovery service = new Discovery("2017-11-07", options);
182+
```
183+
184+
```java
185+
// after instantiation, letting the SDK manage the IAM token
186+
Discovery service = new Discovery("2017-11-07");
187+
IamOptions options = new IamOptions.Builder()
188+
.apiKey("<iam_api_key>")
189+
.url("<iam_url>")
190+
.build();
191+
service.setIamCredentials(options);
192+
```
193+
194+
```java
195+
// in the constructor, taking control of managing IAM token
196+
IamOptions options = new IamOptions.Builder()
197+
.accessToken("<access_token>")
198+
.url("<iam_url>")
199+
.build();
200+
Discovery service = new Discovery("2017-11-07", options);
201+
```
202+
203+
```java
204+
// after instantiation, taking control of managing IAM token
205+
Discovery service = new Discovery("2017-11-07");
206+
IamOptions options = new IamOptions.Builder()
207+
.accessToken("<access_token>")
208+
.url("<iam_url>")
209+
.build();
210+
service.setIamCredentials(options);
211+
```
212+
213+
If at any time you would like to let the SDK take over managing your IAM token, simply override your stored IAM credentials with an IAM API key by calling the `setIamCredentials()` method again.
137214

138215
## Android
139216

0 commit comments

Comments
 (0)