Skip to content

Token Management Client for Java

Introduction

The Token Management Java client is used to retrieve tokens, which allow applications to access IoT data on customer tenants to whom the application has been provided.

Refer to Token Management Service for more information about the service.

Info

The Token Management Service is currently under development in region Europe 2.

Hint

Placeholders in the following samples are indicated by angular brackets < >.

Token Management Operations

Client Name: TokenManagerClient

Token Expiry

Tokens are valid for 30 minutes. Cache and reuse tokens until their expiry.

Get a Token using Environment Variables

This operation fetches the following parameters from environment variables:

Environment Variable Description
MDSP_OS_VM_APP_NAME The application name as stored by the version management of the Operator Services (OS_VM).
MDSP_OS_VM_APP_VERSION The application version as stored by the version management of the Operator Services (OS_VM).
MDSP_KEY_STORE_CLIENT_ID The client ID, which is part of the service credentials.
MDSP_KEY_STORE_CLIENT_SECRET The client secret, which is part of the service credentials.

The MDSP_HOST_TENANT and MDSP_USER_TENANT parameters are obtained from the user token passed via a MindsphereCredentials object.

Environment Variables

On developer tenants the environment variables must be set manually in the Cloud Foundry environment using the command cf env or in the manifest.yml. On operator tenants they are set automatically during application registration.

Credentials

Credentials differ between operator and developer tenants and should not be hard-coded in the final application. You are responsible for keeping the credentials safe.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// Construct the MindsphereCredentials object using the user token
MindsphereCredentials credentials = MindsphereCredentials.userTokenBuilder()
                                        .authorization(<user_token>)
                                        .build();

// Construct the TokenManagerClient object
TokenManagerClient token_manager_client = TokenManagerClient.builder()
                    .mindsphereCredentials(credentials)
                    .restClientConfig(<config>)
                    .build();

AccessTokenResponse access_token_response = null;

try {
  access_token_response = token_manager_client.getToken();
} catch (MindsphereException e) {
  // Exception handling
}

Get a Token using Service Credentials

Get a token providing the client ID and client secret in the request. The application name and version are automatically fetched from environment variables.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Construct the MindsphereCredentials object and supply the user token as shown above

// Construct the TokenManagerClient object as shown above
AccessTokenResponse access_token_response = null;

try {
  access_token_response = token_manager_client.getToken(<client_id>,<client_secret>);
} catch (MindsphereException e) {
  // Exception handling
}

Get a Token without using Environment Variables

Get a token providing the application name and version, the client ID and the client secret in the request.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Construct the MindsphereCredentials object and supply the user token as shown above

// Construct the TokenManagerClient object as shown above
AccessTokenResponse access_token_response = null;

try {
  access_token_response = token_manager_client.getToken(<application_name>,<application_version>,<client_id>,<client_secret>);
} catch (MindsphereException e) {
  // Exception handling
}

Any questions left?

Ask the community


Except where otherwise noted, content on this site is licensed under the MindSphere Development License Agreement.