Skip to content

Commit 2becdcd

Browse files
SirSpideyanweshan
authored andcommitted
Update authentication section for clarity (#720)
Addresses changes requested from reviewers to make the authentication section clearer as we migrate services to IAM. Makes README more consistent with other supported SDKs. ### Summary - Update intro to authentication - Move IAM to top of the stack of auth methods. Rename to just `IAM` - Update getting credentials section - Move usage section to top - Remove examples section (covered in usage) - Update Visual Recognition note - Change headings to sentence case - Add `Watson services` h2 section to correspond to existing TOC - Update TOC - Add existing h2 `documentation`
1 parent daf94f3 commit 2becdcd

File tree

1 file changed

+74
-53
lines changed

1 file changed

+74
-53
lines changed

README.md

Lines changed: 74 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ Node.js client library to use the Watson APIs.
1212
<summary>Table of Contents</summary>
1313

1414
* [Installation](#installation)
15-
* [Getting the Service Credentials](#getting-the-service-credentials)
1615
* [Usage](#usage)
1716
* [Client-side usage](#client-side-usage)
18-
* [Sending Request Headers](#sending-request-headers)
19-
* [Parsing HTTP Response](#parsing-http-response)
17+
* [Authentication](#authentication)
18+
* [IAM](#iam)
19+
* [Username and password](#username-and-password)
20+
* [API key](#api-key)
21+
* [Sending request headers](#sending-request-headers)
22+
* [Parsing HTTP response](#parsing-http-response)
2023
* [Data collection opt-out](#data-collection-opt-out)
24+
* [Documentation](#documentation)
2125
* [Questions](#questions)
22-
* [Examples](#examples)
23-
* [IBM Watson Services](#ibm-watson-services)
26+
* [IBM Watson services](#ibm-watson-services)
2427
* [Authorization](#authorization)
2528
* [Assistant](#assistant)
2629
* [Discovery](#discovery)
@@ -47,35 +50,54 @@ Node.js client library to use the Watson APIs.
4750
npm install watson-developer-cloud
4851
```
4952

50-
## Getting the service credentials
53+
## Usage
5154

52-
## Visual Recognition
55+
The [examples][examples] folder has basic and advanced examples. The examples within each service assume that you already have [service credentials](#getting-credentials).
5356

54-
The process for authenticating with Visual Recognition has changed:
57+
Credentials are checked for in the following order:
5558

56-
- For new service instances, authenticate by using IAM. See [IAM Authentication](#iam-authentication). Also set the service URL by passing in `url` in the service constructor.
57-
- For service instances created before May 23, 2018, authenticate by providing the basic auth `api_key` for the service instance.
59+
1. Hard-coded or programatic credentials passed to the service constructor
5860

59-
### Basic Auth
60-
You will need the `username`, `password`, and `url` for each service. Visual Recognition instances created before May 23, 2018 use `api_key`. Service credentials are different from your IBM Cloud account username and password.
61+
2. Environment variables:
62+
- `SERVICE_NAME_USERNAME` and `SERVICE_NAME_PASSWORD` environment properties (or `SERVICE_NAME_API_KEY` when appropriate) and, optionally, `SERVICE_NAME_URL`
63+
- If using IAM: `SERVICE_NAME_IAM_APIKEY` and optionally `SERVICE_NAME_IAM_URL`, or `SERVICE_NAME_IAM_ACCESS_TOKEN`
6164

62-
To get your service credentials, follow these steps:
65+
3. IBM-Cloud-supplied credentials (via the `VCAP_SERVICES` JSON-encoded environment property)
6366

64-
1. Log in to IBM Cloud at https://console.bluemix.net/catalog/?category=watson.
65-
1. In the IBM Cloud **Catalog**, select the service you want to use.
66-
1. Type a unique name for the service instance in the **Service name** field. For example, type `my-service-name`. Leave the default values for the other options.
67-
1. Click **Create**.
68-
1. From the service dashboard, click **Service credentials**.
69-
1. Click **View credentials** under **Actions**.
70-
1. Copy `username`, `password` (or `api_key` for Visual Recognition), and `url`.
67+
If you run your app in IBM Cloud, the SDK gets credentials from the [`VCAP_SERVICES`][vcap_services] environment variable.
7168

72-
### IAM Authentication
69+
### Client-side usage
70+
71+
See the `examples/` folder for [Browserify](http://browserify.org/) and [Webpack](http://webpack.github.io/) client-side SDK examples (with server-side generation of auth tokens.)
72+
73+
Note: not all services currently support CORS, and therefore not all services can be used client-side.
74+
Of those that do, most require an auth token to be generated server-side via the [Authorization Service](#authorization).
7375

74-
When authenticating with IAM, you have the option of passing in:
75-
- the IAM API key and, optionally, the IAM service URL
76-
- an IAM access token
76+
## Authentication
77+
Watson services are migrating to token-based Identity and Access Management (IAM) authentication.
7778

78-
**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.
79+
- With some service instances, you authenticate to the API by using **[IAM](#iam)**.
80+
- In other instances, you authenticate by providing the **[username and password](#username-and-password)** for the service instance.
81+
- 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).
82+
83+
### Getting credentials
84+
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:
85+
86+
1. Go to the IBM Cloud **[Dashboard][watson-dashboard]** page.
87+
1. Either click an existing Watson service instance or click **Create**.
88+
1. Click **Show** to view your service credentials.
89+
1. Copy the `url` and either `apikey` or `username` and `password`.
90+
91+
### IAM
92+
93+
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.
94+
95+
You supply either an IAM service **API key** or an **access token**:
96+
97+
- 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.
98+
- 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.
99+
100+
#### Supplying the IAM API key
79101

80102
```js
81103
// in the constructor, letting the SDK manage the IAM token
@@ -87,6 +109,8 @@ const discovery = new DiscoveryV1({
87109
});
88110
```
89111

112+
#### Supplying the access token
113+
90114
```js
91115
// in the constructor, assuming control of managing IAM token
92116
const discovery = new DiscoveryV1({
@@ -106,32 +130,32 @@ const discovery = new DiscoveryV1({
106130
discovery.setAccessToken('<access-token>')
107131
```
108132

109-
## Usage
110-
111-
The examples below assume that you already have service credentials. If not,
112-
you will have to create a service in [IBM Cloud][ibm_cloud].
113-
114-
If you are running your application in IBM Cloud, you don't need to specify the
115-
credentials; the library will get them for you by looking at the `VCAP_SERVICES` environment variable.
133+
### Username and password
116134

117-
Credentials are checked for in the following order:
118-
119-
1. Hard-coded or programatic credentials passed to the service constructor
135+
```javascript
136+
var DiscoveryV1 = require('watson-developer-cloud/discovery/v1');
120137

121-
2. Environment variables:
122-
- `SERVICE_NAME_USERNAME` and `SERVICE_NAME_PASSWORD` environment properties (or `SERVICE_NAME_API_KEY` when appropriate) and, optionally, `SERVICE_NAME_URL`
123-
- If using IAM: `SERVICE_NAME_IAM_APIKEY` and optionally `SERVICE_NAME_IAM_URL`, or `SERVICE_NAME_IAM_ACCESS_TOKEN`
138+
var discovery = new DiscoveryV1({
139+
version: '{version}',
140+
username: '{username}',
141+
password: '{password}'
142+
});
143+
```
124144

125-
3. IBM-Cloud-supplied credentials (via the `VCAP_SERVICES` JSON-encoded environment property)
145+
### API key
126146

127-
### Client-side usage
147+
**Important**: This type of authentication works only with Visual Recognition instances created before May 23, 2018. Newer instances of Visual Recognition use [IAM](#iam).
128148

129-
See the `examples/` folder for [Browserify](http://browserify.org/) and [Webpack](http://webpack.github.io/) client-side SDK examples (with server-side generation of auth tokens.)
149+
```javascript
150+
var VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3');
130151

131-
Note: not all services currently support CORS, and therefore not all services can be used client-side.
132-
Of those that do, most require an auth token to be generated server-side via the [Authorization Service](#authorization).
152+
var visualRecognition = new VisualRecognitionV3({
153+
version: '{version}',
154+
api_key: '{api_key}'
155+
});
156+
```
133157

134-
### Sending Request Headers
158+
### Sending request headers
135159

136160
Custom headers can be passed with any request. Each method has an optional parameter `headers` which can be used to pass in these custom headers, which can override headers that we use as parameters.
137161

@@ -159,7 +183,7 @@ assistant.message({
159183

160184
```
161185

162-
### Parsing HTTP Response
186+
### Parsing HTTP response
163187

164188
To retrieve the HTTP response, all methods can be called with a callback function with three parameters, with the third being the response. Users for example may retrieve the response headers with this usage pattern.
165189

@@ -198,14 +222,11 @@ You can find links to the documentation at https://console.bluemix.net/developer
198222

199223
There are also auto-generated JSDocs available at http://watson-developer-cloud.github.io/node-sdk/master/
200224

201-
202225
## Questions
203226

204227
If you are having difficulties using the APIs or have a question about the Watson services, please ask a question at [dW Answers](https://developer.ibm.com/answers/questions/ask/?topics=watson) or [Stack Overflow](http://stackoverflow.com/questions/ask?tags=ibm-watson-cognitive).
205228

206-
## Examples
207-
208-
The [examples][examples] folder has basic and advanced examples.
229+
## IBM Watson services
209230

210231
### Authorization
211232

@@ -656,7 +677,7 @@ Running a specific test:
656677
$ mocha -g '<test name>'
657678
```
658679
659-
## Open Source @ IBM
680+
## Open source @ IBM
660681
[Find more open source projects on the IBM Github Page.](http://ibm.github.io/)
661682
662683
## License
@@ -677,14 +698,14 @@ See [CONTRIBUTING](https://github.com/watson-developer-cloud/node-sdk/blob/maste
677698
[text_to_speech]: https://www.ibm.com/watson/services/text-to-speech/
678699
[speech_to_text]: https://www.ibm.com/watson/services/speech-to-text/
679700
[language_translator]: https://www.ibm.com/watson/services/language-translator/
680-
681701
[ibm_cloud]: https://console.bluemix.net
702+
[watson-dashboard]: https://console.bluemix.net/dashboard/apps?category=watson
682703
[npm_link]: https://www.npmjs.com/package/watson-developer-cloud
683704
[request_github]: https://github.com/request/request
684705
[dialog_migration]: https://console.bluemix.net/docs/services/conversation/index.html
685-
686706
[examples]: https://github.com/watson-developer-cloud/node-sdk/tree/master/examples
687707
[document_conversion_integration_example]: https://github.com/watson-developer-cloud/node-sdk/tree/master/examples/document_conversion_integration.v1.js
688708
[conversation_tone_analyzer_example]: https://github.com/watson-developer-cloud/node-sdk/tree/master/examples/conversation_tone_analyzer_integration
689-
690709
[license]: http://www.apache.org/licenses/LICENSE-2.0
710+
[vcap_services]: https://console.bluemix.net/docs/services/watson/getting-started-variables.html
711+

0 commit comments

Comments
 (0)