AWS Api网关JAVA SDK将使用计划添加到Api密钥
以下代码将在AWS API网关中创建新的API密钥。为了好玩,我还得到了一个名为“Basic”的现有使用计划,id为“1234” 就我的一生而言,我不知道如何获取我新创建的API密钥并将现有的使用计划添加到其中。这可以通过“添加到使用计划”按钮在门户网站上手动完成,但我想将我的新用户添加到免费计划中AWS Api网关JAVA SDK将使用计划添加到Api密钥,java,api,amazon-web-services,Java,Api,Amazon Web Services,以下代码将在AWS API网关中创建新的API密钥。为了好玩,我还得到了一个名为“Basic”的现有使用计划,id为“1234” 就我的一生而言,我不知道如何获取我新创建的API密钥并将现有的使用计划添加到其中。这可以通过“添加到使用计划”按钮在门户网站上手动完成,但我想将我的新用户添加到免费计划中 BasicAWSCredentials awsCreds = new BasicAWSCredentials(aws_id, aws_key); apiGateway =
BasicAWSCredentials awsCreds = new BasicAWSCredentials(aws_id, aws_key);
apiGateway = AmazonApiGatewayClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(Regions.US_EAST_1).build();
CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest();
createApiKeyRequest.setName("awesome company);
createApiKeyRequest.setEnabled(true);
createApiKeyRequest.setCustomerId("someid");
CreateApiKeyResult result = apiGateway.createApiKey(createApiKeyRequest);
GetUsagePlanRequest getUsagePlanRequest = new GetUsagePlanRequest();
getUsagePlanRequest.setUsagePlanId("1234");
GetUsagePlanResult getUsagePlanResult = apiGateway.getUsagePlan(getUsagePlanRequest);
任何AWS SDK专家都知道如何将使用计划连接到api密钥?这是我的帖子的解决方案-密钥类型为“api_密钥”在任何地方都没有记录,我在一些随机的python示例中找到了它:/n这将创建一个具有api密钥的新用户,并使用api网关java SDK将其添加到使用计划中
BasicAWSCredentials awsCreds = new BasicAWSCredentials(aws_id, aws_key);
apiGateway = AmazonApiGatewayClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(Regions.US_EAST_1).build();
CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest();
createApiKeyRequest.setName("My awesome new user");
createApiKeyRequest.setEnabled(true);
createApiKeyRequest.setCustomerId(UUID.randomUUID().toString());
CreateApiKeyResult result = apiGateway.createApiKey(createApiKeyRequest);
GetUsagePlanRequest getUsagePlanRequest = new GetUsagePlanRequest();
getUsagePlanRequest.setUsagePlanId(BASIC_USAGE_PLAN_ID);
CreateUsagePlanKeyRequest createUsagePlanKeyRequest = new CreateUsagePlanKeyRequest()
.withUsagePlanId(BASIC_USAGE_PLAN_ID);
createUsagePlanKeyRequest.setKeyId(result.getId());
createUsagePlanKeyRequest.setKeyType("API_KEY");
apiGateway.createUsagePlanKey(createUsagePlanKeyRequest);
这也许应该是一个注释,但我为可读性做了一个回答(键类型是有文档记录的) 注意,此示例没有try-catch块
// Client
AmazonApiGateway client = AmazonApiGatewayClientBuilder.standard().withRegion("my region here").build();
// Create new key
CreateApiKeyRequest keyReq = new CreateApiKeyRequest();
keyReq.setName("key name");
keyReq.setDescription("description");
keyReq.setEnabled(true);
CreateApiKeyResult keyRes = client.createApiKey(keyReq);
// Use existing plan
CreateUsagePlanKeyRequest planReq = new CreateUsagePlanKeyRequest();
planReq.setUsagePlanId("my usage plan id");
planReq.setKeyId(keyRes.getId()); // id from new key
planReq.setKeyType("API_KEY");
// add key to plan
client.createUsagePlanKey(planReq);