- Node.js >= 12
npm install -S @volcengine/openapi
提供三种设置方式
// 使用默认的service实例。你也可以创建一个新实例。
// `const iamService = new iam.IamService();`
const iamService = iam.defaultService;
// 设置aksk
iamService.setAccessKeyId(AccessKeyId);
iamService.setSecretKey(SecretKey);
// 如果使用sts进行请求,设置完成aksk之后还需要设置一下`SessionToken`
iamService.setSessionToken(SessionToken);
VOLC_ACCESSKEY="your ak" VOLC_SECRETKEY="your sk"
以json格式放在~/.volc/config
中,格式为:
{"VOLC_ACCESSKEY":"your ak","VOLC_SECRETKEY":"your sk"}
以调用iam
服务的ListUsers
API为例
import { iam } from '@volcengine/openapi';
async main(AccessKeyId, SecretKey) {
// 使用默认的service实例。你也可以创建一个新实例。
// `const iamService = new iam.IamService();`
const iamService = iam.defaultService;
// 设置aksk
iamService.setAccessKeyId(AccessKeyId);
iamService.setSecretKey(SecretKey);
// 请求预定义的OpenAPI
const usersResponse = await iamService.ListUsers({
Limit: 10,
Offset: 0,
});
}
import {Signer} from '@volcengine/openapi';
// 请求数据
const openApiRequestData: RequestObj = {
region: 'cn-north-1',
method: 'GET',
// [可选] http request url query
params: {},
// http request headers
headers: {},
// [可选] http request body
body: "",
}
const signer = new Signer(openApiRequestData, "iam");
// 签名
signer.addAuthorization({accessKeyId, secretKey, sessionToken});
// 打印签名后的 headers
console.log(openApiRequestData.headers);
import {Signer} from '@volcengine/openapi';
// 请求数据
const openApiRequestData: RequestObj = {
method: "POST",
region: "cn-north-1",
params: {
Action: "AssumeRole",
Version: "2018-01-01",
RoleTrn: "trn:iam::200:role/STSRole",
RoleSessionName: "test",
},
}
const credentials: Credentials = {
accessKeyId: VOLC_ACCESSKEY,
secretKey: VOLC_SECRETKEY,
sessionToken: "",
}
const signer = new Signer(openApiRequestData, "sts");
// 最终经过加签的 HTTP Query Params
const signedQueryString = signer.getSignUrl(credentials);