Skip to content

Commit a228e7d

Browse files
authored
Merge pull request #954 from watson-developer-cloud/SirSpidey-iam-patch-1
Update authentication section for clarity
2 parents f55fac7 + 641b845 commit a228e7d

File tree

1 file changed

+70
-63
lines changed

1 file changed

+70
-63
lines changed

README.md

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Java client library to use the [Watson APIs][wdc].
1616
* [Usage](#usage)
1717
* [Running in IBM Cloud](#running-in-ibm-cloud)
1818
* [Authentication](#authentication)
19-
* [Username and Password](#username-and-password)
20-
* [API Key](#api-key)
21-
* [Using IAM](#using-iam)
19+
* [IAM](#iam)
20+
* [Username and password](#username-and-password)
21+
* [API key](#api-key)
2222
* IBM Watson Services
2323
* [Assistant](assistant)
2424
* [Discovery](discovery)
@@ -32,12 +32,12 @@ Java client library to use the [Watson APIs][wdc].
3232
* [Tradeoff Analytics](tradeoff-analytics)
3333
* [Visual Recognition](visual-recognition)
3434
* [Android](#android)
35-
* [Using a Proxy](#using-a-proxy)
36-
* [Default Headers](#default-headers)
37-
* [Sending Request Headers](#sending-request-headers)
38-
* [Parsing HTTP Response Info](#parsing-http-response-info)
39-
* [Specifying a Service URL](#specifying-a-service-url)
40-
* [401 Unauthorized Error](#401-unauthorized-error)
35+
* [Using a proxy](#using-a-proxy)
36+
* [Default headers](#default-headers)
37+
* [Sending request headers](#sending-request-headers)
38+
* [Parsing HTTP response info](#parsing-http-response-info)
39+
* [Specifying a service URL](#specifying-a-service-url)
40+
* [401 unauthorized error](#401-unauthorized-error)
4141
* [Changes for v4.0](#changes-for-v40)
4242
* [Debug](#debug)
4343
* [Eclipse and Intellij](#working-with-eclipse-and-intellij-idea)
@@ -84,7 +84,7 @@ Only Assistant:
8484
'com.ibm.watson.developer_cloud:assistant:6.1.0'
8585
```
8686

87-
##### Development Snapshots
87+
##### Development snapshots
8888

8989
Snapshots of the development version are available in [Sonatype's snapshots repository][sonatype_snapshots].
9090

@@ -116,7 +116,7 @@ Now, you are ready to see some [examples](https://github.com/watson-developer-cl
116116
## Usage
117117

118118
The examples within each service assume that you already have service credentials. If not,
119-
you will have to create a service in [IBM Cloud][ibm_cloud].
119+
you will have to [create a service](#getting-credentials) in IBM Cloud.
120120

121121
If you are running your application in IBM Cloud (or other platforms based on Cloud Foundry), you don't need to specify the
122122
credentials; the library will get them for you by looking at the [`VCAP_SERVICES`][vcap_services] environment variable.
@@ -128,54 +128,33 @@ If you have more than one plan, you can use `CredentialUtils` to get the service
128128

129129
## Authentication
130130

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.
131+
Watson services are migrating to token-based Identity and Access Management (IAM) authentication.
132132

133-
Getting the credentials necessary for authentication is the same process for all methods. To get them, follow these steps:
133+
- With some service instances, you authenticate to the API by using **[IAM](#iam)**.
134+
- In other instances, you authenticate by providing the **[username and password](#username-and-password)** for the service instance.
135+
- Visual Recognition uses a form of [API key](#api-key) only with instances created before May 23, 2018. Newer instances of Visual Recognition use [IAM](#iam).
134136

135-
1. Log in to [IBM Cloud](https://console.bluemix.net/catalog?category=watson)
136-
1. In the IBM Cloud **Catalog**, select the service you want to use.
137-
1. Click **Create**.
138-
1. On the left side of the page, click **Service Credentials**, and then **View credentials** to view your service credentials.
139-
1. Copy the necessary credentials (`url`, `username`, `password`, `api_key`, `apikey`, etc.).
137+
### Getting credentials
138+
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:
140139

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:
140+
1. Go to the IBM Cloud **[Dashboard][watson-dashboard]** page.
141+
1. Either click an existing Watson service instance or click **Create**.
142+
1. Click **Show** to view your service credentials.
143+
1. Copy the `url` and either `apikey` or `username` and `password`.
142144

143-
### Username and Password
145+
In your code, you can use these values in the service constructor or with a method call after instantiating your service.
144146

145-
```java
146-
// in the constructor
147-
Discovery service = new Discovery("2017-11-07", "<username>", "<password>");
148-
```
147+
### IAM
149148

150-
```java
151-
// after instantiation
152-
Discovery service = new Discovery("2017-11-07");
153-
service.setUsernameAndPassword("<username>", "<password>");
154-
```
149+
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.
155150

156-
### API Key
151+
You supply either an IAM service **API key** or an **access token**:
157152

158-
_Important: Instantiation with API key works only with Visual Recognition service instances created before May 23, 2018. Visual Recognition instances created after May 22 use IAM._
153+
- 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.
154+
- Use the access token if you want to manage the lifecycle yourself. Access tokens are valid. For details, see [Authenticating with IAM tokens](https://console.bluemix.net/docs/services/watson/getting-started-iam.html). If you want to switch to API key, override your stored IAM credentials with an IAM API key. Then call the `setIamCredentials()` method again.
159155

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:
174-
- the IAM API key and, optionally, the IAM service URL
175-
- an IAM access token
176-
177-
**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.
178156

157+
#### Supplying the IAM API key
179158
```java
180159
// in the constructor, letting the SDK manage the IAM token
181160
IamOptions options = new IamOptions.Builder()
@@ -194,6 +173,7 @@ IamOptions options = new IamOptions.Builder()
194173
service.setIamCredentials(options);
195174
```
196175

176+
#### Supplying the access token
197177
```java
198178
// in the constructor, assuming control of managing IAM token
199179
IamOptions options = new IamOptions.Builder()
@@ -211,13 +191,39 @@ IamOptions options = new IamOptions.Builder()
211191
service.setIamCredentials(options);
212192
```
213193

214-
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.
194+
### Username and password
195+
196+
```java
197+
// in the constructor
198+
Discovery service = new Discovery("2017-11-07", "<username>", "<password>");
199+
```
200+
201+
```java
202+
// after instantiation
203+
Discovery service = new Discovery("2017-11-07");
204+
service.setUsernameAndPassword("<username>", "<password>");
205+
```
206+
207+
### API key
208+
209+
**Important**: This type of authentication works only with Visual Recognition instances created before May 23, 2018. Newer instances of Visual Recognition use [IAM](#iam).
210+
211+
```java
212+
// in the constructor
213+
VisualRecognition service = new VisualRecognition("2016-05-20", "<api_key>");
214+
```
215+
216+
```java
217+
// after instantiation
218+
VisualRecognition service = new VisualRecognition("2016-05-20");
219+
service.setApiKey("<api_key>");
220+
```
215221

216222
## Android
217223

218224
The Android SDK utilizes the Java SDK while making some Android-specific additions. This repository can be found [here](https://github.com/watson-developer-cloud/android-sdk). It depends on [OkHttp][] and [gson][].
219225

220-
## Using a Proxy
226+
## Using a proxy
221227

222228
Override the `configureHttpClient()` method and add the proxy using the `OkHttpClient.Builder` object.
223229

@@ -246,7 +252,7 @@ String apiKey = CredentialUtils.getAPIKey(service.getName(), CredentialUtils.PLA
246252
service.setApiKey(apiKey);
247253
```
248254

249-
## Sending Request Headers
255+
## Sending request headers
250256

251257
Custom headers can be passed with any request. To do so, add the header to the `ServiceCall` object before executing the request. For example, this is what it looks like to send the header `Custom-Header` along with a call to the Watson Assistant service:
252258

@@ -256,7 +262,7 @@ WorkspaceCollection workspaces = service.listWorkspaces()
256262
.execute();
257263
```
258264

259-
## Parsing HTTP Response Info
265+
## Parsing HTTP response info
260266

261267
The basic `execute()`, `enqueue()`, and `rx()` methods make HTTP requests to your Watson service and return models based on the requested endpoint. If you would like access to some HTTP response information along with the response model, you can use the more detailed versions of those three methods: `executeWithDetails()`, `enqueueWithDetails()`, and `rxWithDetails()`. To capture the responses, use the new `Response<T>` class, with `T` being the expected response model.
262268

@@ -287,7 +293,7 @@ service.listWorkspaces().enqueueWithDetails(new ServiceCallbackWithDetails<Works
287293
});
288294
```
289295

290-
## Default Headers
296+
## Default headers
291297

292298
Default headers can be specified at any time by using the `setDefaultHeaders(Map<String, String> headers)` method.
293299

@@ -304,11 +310,11 @@ service.setDefaultHeaders(headers);
304310
// All the api calls from now on will send the default headers
305311
```
306312

307-
## Specifying a Service URL
313+
## Specifying a service URL
308314

309-
You can set the correct API Endpoint for your service calling `setEndPoint()`.
315+
You can set the correct API endpoint for your service calling `setEndPoint()`.
310316

311-
For example, if you have the Discovery service in Germany, the Endpoint may be `https://gateway-fra.watsonplatform.net/discovery/api`.
317+
For example, if you have the Discovery service in Germany, the endpoint may be `https://gateway-fra.watsonplatform.net/discovery/api`.
312318

313319
You will need to call
314320

@@ -317,10 +323,10 @@ Discovery service = new Discovery("2017-11-07");
317323
service.sentEndPoint("https://gateway-fra.watsonplatform.net/discovery/api")
318324
```
319325

320-
## 401 Unauthorized Error
326+
## 401 unauthorized error
321327

322328
Make sure you are using the service credentials and not your IBM Cloud account/password.
323-
Check the API Endpoint, you may need to update the default using `setEndPoint()`.
329+
Check the API endpoint, you may need to update the default using `setEndPoint()`.
324330

325331
## Changes for v4.0
326332
Version 4.0 focuses on the move to programmatically-generated code for many of the services. See the [changelog](https://github.com/watson-developer-cloud/java-sdk/wiki/Changelog) for the details. This version also includes many breaking changes as a result of standardizing behavior across the new generated services. Full details on migration from previous versions can be found [here](https://github.com/watson-developer-cloud/java-sdk/wiki/Migration).
@@ -356,7 +362,7 @@ INFO: <-- 200 OK https://gateway.watsonplatform.net/tradeoff-analytics/api/v1/di
356362

357363
**Warning:** The logs generated by this logger when using the level `FINE` or `ALL` has the potential to leak sensitive information such as "Authorization" or "Cookie" headers and the contents of request and response bodies. This data should only be logged in a controlled way or in a non-production environment.
358364

359-
## Build + Test
365+
## Build + test
360366

361367
To build and test the project you can use [Gradle][] (version 1.x).
362368

@@ -381,7 +387,7 @@ gradle idea # Intellij IDEA
381387
gradle eclipse # Eclipse
382388
```
383389

384-
## Open Source @ IBM
390+
## Open source @ IBM
385391

386392
Find more open source projects on the [IBM Github Page](http://ibm.github.io/)
387393

@@ -394,7 +400,7 @@ available in [LICENSE](LICENSE).
394400

395401
See [CONTRIBUTING.md](.github/CONTRIBUTING.md).
396402

397-
## Code of Conduct
403+
## Code of conduct
398404

399405
See [CODE_OF_CONDUCT.md](.github/CODE_OF_CONDUCT.md).
400406

@@ -408,12 +414,13 @@ or [Stack Overflow](http://stackoverflow.com/questions/ask?tags=ibm-watson).
408414

409415
[wdc]: https://www.ibm.com/watson/developer/
410416
[ibm_cloud]: https://console.bluemix.net
417+
[watson-dashboard]: https://console.bluemix.net/dashboard/apps?category=watson
411418
[Gradle]: http://www.gradle.org/
412419
[OkHttp]: http://square.github.io/okhttp/
413420
[gson]: https://github.com/google/gson
414421
[apache_maven]: http://maven.apache.org/
415422
[sonatype_snapshots]: https://oss.sonatype.org/content/repositories/snapshots/com/ibm/watson/developer_cloud/
416-
[vcap_services]: https://docs.run.pivotal.io/devguide/deploy-apps/environment-variable.html#VCAP-SERVICES
423+
[vcap_services]: https://console.bluemix.net/docs/services/watson/getting-started-variables.html
417424

418425

419426
[jar]: https://github.com/watson-developer-cloud/java-sdk/releases/download/java-sdk-6.1.0/java-sdk-6.1.0-jar-with-dependencies.jar

0 commit comments

Comments
 (0)