AWS Api Gateway JAVA SDK将使用计划添加到API密钥

时间:2017-06-29 19:39:24

标签: java api amazon-web-services

以下代码将在AWS API Gateway中创建新的API KEY。为了好玩,我还得到了一个名为" Basic"的现有使用计划。 id为" 1234"

对于我的生活,我无法了解如何获取新创建的API密钥并将现有的使用计划添加到其中。这可以在Web门户上通过"添加到使用计划"手动完成。按钮,但我想将我的新用户添加到免费计划。

 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密钥?

2 个答案:

答案 0 :(得分:1)

这是我的帖子的解决方案 - 关键类型是“API_KEY”没有在任何地方记录,我在一些随机的python示例中找到它:/这创建了一个带有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);

答案 1 :(得分:1)

这可能应该是评论,但我为可读性做了回答(密钥类型记录为here)。

// 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);

注意,此示例没有try-catch块