You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+82-5Lines changed: 82 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,10 @@ Java client library to use the [Watson APIs][wdc].
15
15
*[Gradle](#gradle)
16
16
*[Usage](#usage)
17
17
*[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)
19
22
* IBM Watson Services
20
23
*[Assistant](assistant)
21
24
*[Discovery](discovery)
@@ -123,17 +126,91 @@ credentials; the library will get them for you by looking at the [`VCAP_SERVICES
123
126
When running in IBM Cloud (or other platforms based on Cloud Foundry), the library will automatically get the credentials from [`VCAP_SERVICES`][vcap_services].
124
127
If you have more than one plan, you can use `CredentialUtils` to get the service credentials for an specific plan.
125
128
126
-
## Getting the Service Credentials
129
+
## Authentication
127
130
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.
129
132
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:
131
134
132
135
1. Log in to [IBM Cloud](https://console.bluemix.net/catalog?category=watson)
133
136
1. In the IBM Cloud **Catalog**, select the service you want to use.
134
137
1. Click **Create**.
135
138
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).
_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 =newVisualRecognition("2016-05-20", "<api_key>");
163
+
```
164
+
165
+
```java
166
+
// after instantiation
167
+
VisualRecognition service =newVisualRecognition("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 =newIamOptions.Builder()
178
+
.apiKey("<iam_api_key>")
179
+
.url("<iam_url>")
180
+
.build();
181
+
Discovery service =newDiscovery("2017-11-07", options);
182
+
```
183
+
184
+
```java
185
+
// after instantiation, letting the SDK manage the IAM token
186
+
Discovery service =newDiscovery("2017-11-07");
187
+
IamOptions options =newIamOptions.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 =newIamOptions.Builder()
197
+
.accessToken("<access_token>")
198
+
.url("<iam_url>")
199
+
.build();
200
+
Discovery service =newDiscovery("2017-11-07", options);
201
+
```
202
+
203
+
```java
204
+
// after instantiation, taking control of managing IAM token
205
+
Discovery service =newDiscovery("2017-11-07");
206
+
IamOptions options =newIamOptions.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.
0 commit comments