令牌管理 Java 客户端¶
简介¶
令牌管理 Java 客户端用于检索令牌,允许应用访问已提供该应用的客户租户的 IoT 数据。
有关该服务的更多信息,请参见 Token Management Service。
信息
Europe 2 区域的Token Management Service 正在开发中。
提示
以下示例中的占位符由尖括号 < >
表示。
令牌管理操作¶
客户端名称:TokenManagerClient
令牌到期
令牌有效期为 30 分钟。将令牌缓存并重复使用,直至它们到期。
通过环境变量获取令牌¶
此操作从环境变量中获取以下参数:
环境变量 | 描述 |
---|---|
MDSP_OS_VM_APP_NAME | 由 Operator Services (OS_VM ) 版本管理存储的应用名称。 |
MDSP_OS_VM_APP_VERSION | 由 Operator Services (OS_VM ) 版本管理存储的应用版本。 |
MDSP_KEY_STORE_CLIENT_ID | 客户端 ID(包含在服务凭证中)。 |
MDSP_KEY_STORE_CLIENT_SECRET | 客户端密钥(包含在服务凭证中)。 |
MDSP_HOST_TENANT
和 MDSP_USER_TENANT
参数通过MindsphereCredentials对象传递的用户令牌获取。
环境变量
对于开发者租户,必须在 Cloud Foundry 环境中使用命令 cf env
或者在 manifest.yml
中手动设置环境变量。对于操作员租户,在应用注册期间自动设置环境变量。
凭证
凭证对于操作员和开发者租户有所不同,并且不应该在最终应用中硬编程。您有责任保持凭证安全。
// 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
}
使用服务凭证获取令牌¶
获取一个在请求中提供客户端 ID 和客户端密钥的令牌。应用名称和版本自动从环境变量中获取。
// 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
}
获取令牌¶
获取一个在请求中提供应用名称和版本、客户端 ID 和客户端密钥的令牌。
// 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
}
Last update: March 22, 2023