使用 API 密钥生成 IBM Cloud IAM 令牌
生成IBM Cloud®Identity and Access Management通过使用您的 IAM API 密钥或服务 ID 的 API 密钥来获取 (IAM) 令牌。IBM Cloud只有获得指定 IAM 角色授权的用户才能访问 API。 每个用户在调用 API 时必须传递 API 的凭证,才能通过认证。
生成 IAM 令牌
您可以使用 IBM Cloud API 密钥或服务标识的 API 密钥来生成 IAM 令牌。 API 密钥是一个永久性凭证,如果您没有丢失 API 密钥值或删除帐户中的 API 密钥,则可以重复使用。 如果要开发的应用程序需要使用其他 IBM Cloud 服务,也可以使用此过程。 要使访问令牌能够传递到每个 IBM Cloud 服务,必须使用服务标识 API 密钥。
访问令牌是一个临时凭证,最迟 1 小时后过期。 获取的token过期后,必须生成新的token才能继续调用IBM Cloud或服务 API,并且您只能执行所有账户中分配的访问级别允许的操作。 使用响应属性 expires_in
在 API 响应中识别您的特定访问令牌的有效时间长度。
使用 API 密钥生成 IAM 令牌
要使用 API 密钥以编程方式生成 IAM 令牌,请调用 IAM 身份服务 API或者 SDKs如以下示例请求所示。
curl -X POST 'https://iam.cloud.ibm.com/identity/token' -H 'Content-Type: application/x-www-form-urlencoded' -d 'grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=MY_APIKEY'
import com.ibm.cloud.sdk.core.security.IamAuthenticator;
import <sdk_base_package>.ExampleService.v1.ExampleService;
...
// Create the authenticator.
IamAuthenticator authenticator = new IamAuthenticator.Builder()
.apikey("myapikey")
.build();
// Create the service instance.
ExampleService service = new ExampleService(authenticator);
// 'service' can now be used to invoke operations.
const ExampleServiceV1 = require('mysdk/example-service/v1');
const { IamAuthenticator } = require('mysdk/auth');
const authenticator = new IamAuthenticator({
apikey: '<iam-api-key>',
});
const myService = new ExampleServiceV1({
authenticator,
});
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from <sdk-package-name>.example_service_v1 import *
# Create the authenticator.
authenticator = IAMAuthenticator('myapikey')
# Construct the service instance.
service = ExampleServiceV1(authenticator=authenticator)
# Use 'service' to invoke operations.
import {
"github.com/IBM/go-sdk-core/v5/core"
"<appropriate-git-repo-url>/exampleservicev1"
}
...
// Create the authenticator.
authenticator := &core.IamAuthenticator{
ApiKey: "myapikey",
}
// Create the service options struct.
options := &exampleservicev1.ExampleServiceV1Options{
Authenticator: authenticator,
}
// Construct the service instance.
service := exampleservicev1.NewExampleServiceV1(options)
// 'service' can now be used to invoke operations.
预期响应
{
"access_token": "eyJhbGciOiJIUz......sgrKIi8hdFs",
"refresh_token": "not_supported",
"ims_user_id": 118...90,
"token_type": "Bearer",
"expires_in": 3600,
"expiration": 1473188353,
"scope": "ibm openid"
}
有关详细信息,请参阅 IAM 身份服务 API。
有关详细信息,请参阅 Java软件开发工具包。
有关详细信息,请参阅 软件开发工具包。
有关详细信息,请参阅 Python软件开发工具包。
有关详细信息,请参阅 Go SDK。
IAM 令牌的有效期最长为 60 分钟,可随时更改。 令牌到期以后,必须生成新令牌。 使用属性 expires_in
您刚刚创建的 IAM 令牌的到期日期。