2017-06-29 196 views
1

以下代碼將在AWS API網關中創建一個新的API密鑰。爲了好玩,我還得到了一個名爲「Basic」的現有使用計劃,其ID爲「1234」AWS Api網關JAVA SDK向API密鑰添加使用計劃

對於我的生活,我找不到如何使用我新創建的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開發工具包專家都知道如何將使用計劃連接到API密鑰?

回答

0

下面是我的文章的解決方案 - 「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);