Watson Machine Learning authentication

To be able to use interfaces such as the Watson Machine Learning REST API and Watson Machine Learning Python client library, you need to authenticate with the Watson Machine Learning repository.

How you authenticate depends on whether you are using a notebook within Watson Studio or whether you are accessing Watson Machine Learning from outside the cluster.

Authenticating from a notebook in Watson Studio

If you are accessing Watson Studio from a notebook, use this method of authentication to protect your authentication credentials.

Get a bearer token

Watson Machine Learning requires a bearer token to authenticate the user calling the REST APIs. The token expires after 12 hours. To access a token when you create a notebook in Watson Studio, use the predefined variable ‘USER_ACCESS_TOKEN’.

Alternatively, you can retrieve a bearer token from the user management service using an API call. This example request uses an API key to retrieve the token:

curl -k -X POST https://cpd_cluster_host/icp4d-api/v1/authorize -H ‘cache-control: no-cache’ -H ‘content-type: application/json’ -d ‘{“username”:“admin”,“password”:“password”}’

Where you specify:

The call returns a JSON snippet from which the bearer token can be extracted from the access_token` field:

  "username": "admin",
  "role": "Admin",
  "permissions": [
  "sub": "admin",
  "iss": "KNOXSSO",
  "aud": "DSX",
  "uid": "999",
  "authenticator": "default",
  "access_token": "eyJraWQiOiIyMDE3MDgwOS0wMDowMDowMCIsImFsZyI6...",
  "_messageCode_": "success"

You can copy and paste the value of the accessToken attribute to your Python program. Using this method you can connect to Watson Machine Learning without having to place your credentials into a program file or notebook.

Use the token to authenticate with the server

Use your user access token to connect to the server. This will mask your user credentials.

import sys,os,os.path
token = os.environ['USER_ACCESS_TOKEN']

wml_credentials = {
"token": token,
"instance_id" : "openshift",
"url": "https://111.22.333.444",
"version": "3.5"


Authenticating the Python client from outside of Watson Studio

If you are not storing your notebook within Watson Studio, you can bypass retrieving a token and authenticate with your Cloud Pak for Data credentials.

wml_credentials = {
    "instance_id" : "openshift",
    "url"         : "https://111.22.333.444",
    "username"    : "MNOPQRS",
    "password"    : "TUVWXYZ",
    "version"     : "3.5"
client = WatsonMachineLearningAPIClient( wml_credentials )

For an example of using authentication with a notebook, see Machine Learning Python samples and examples.