skip to Main Content

I have problem initializing API Gateway Client in Java using AWS-Java-SDK (tried old and newest version). I’ve checked 10 times if my access and secret keys are correct and they are. I’m not sure what i bad coded so it stops working after "Good 2" output. Need help please

Here’s my code:

        public void startAPIGateway() {

        callbacks.printOutput("Starting API Gateway");
        
        String API_NAME = "Testing";

        
        BasicAWSCredentials credentials = new BasicAWSCredentials(accessKeyField.getText(), secretKeyField.getText());
        callbacks.printOutput("Good 1");
        // Create an AWSStaticCredentialsProvider object with the credentials
        AWSStaticCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
        callbacks.printOutput("Good 2");
        
        // Create an AwsClientBuilder object with the region and endpoint;
        AmazonApiGatewayClientBuilder clientBuilder = AmazonApiGatewayClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("https://apigateway.eu-east-2.amazonaws.com", "eu-east-2"));
        callbacks.printOutput("Good 3");
        // Set the credentials provider on the client builder
        clientBuilder.setCredentials(credentialsProvider);
        clientBuilder.setRegion("eu-east-2");

        
        AmazonApiGateway client = clientBuilder.build();

        
        callbacks.printOutput("Goodzzzz");
        
        
        CreateRestApiRequest restApiRequest = new CreateRestApiRequest().withName(API_NAME);
        CreateRestApiResult createRestApiResult = client.createRestApi(restApiRequest);

callbacks.printOutput(createRestApiResult.getId());

// Add a new resource with a path variable
GetResourcesRequest getResourcesRequest = new GetResourcesRequest()
        .withRestApiId(createRestApiResult.getId());


GetResourcesResult getResourcesResult = client.getResources(getResourcesRequest);

CreateResourceRequest createResourceRequest = new CreateResourceRequest()
        .withRestApiId(createRestApiResult.getId())
        .withParentId(getResourcesResult.getItems().get(0).getId())
        .withPathPart("{proxy+}");

CreateResourceResult createResourceResult = client.createResource(createResourceRequest);
        
    }

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Installed AWS SDK for Java 2 by Maven and added this code, still my extension for Burp is not working:

         public void getKeys(ApiGatewayClient apiGateway) {
    
                try {
                    GetApiKeysResponse response = apiGateway.getApiKeys();
                    List<ApiKey> keys = response.items();
                    for (ApiKey key: keys) {
                        callbacks.printOutput("key id is: "+key.id());
                        callbacks.printOutput("key name is: "+key.name());
                    }
    
                } catch (ApiGatewayException e) {
                    callbacks.printOutput(e.awsErrorDetails().errorMessage());
                    System.exit(1);
                }
            }
    
         
        public void startAPIGateway() {
            
            callbacks.printOutput("Starting API Gateway");
    
            Region region = Region.EU_WEST_2;
    
            AwsBasicCredentials awsCreds = AwsBasicCredentials.create(
                    accessKeyField.getText(),
                    secretKeyField.getText());
            
            StaticCredentialsProvider staticCredentials = StaticCredentialsProvider.create(awsCreds);
            
            ApiGatewayClient client = ApiGatewayClient.builder()
                        .region(region)
                        .credentialsProvider(staticCredentials)
                        .build();
    
            
             callbacks.printOutput("Good 1");
             
                getKeys(client);
                client.close();
           
            
            
        }
    

    Resulted with this: enter image description here


  2. You are using the old Java V1 API. This is no longer recommended to use by the AWS SDK Java team. You should consider moving to AWS SDK for Java V2. You can read about this SDK version here:

    Developer guide – AWS SDK for Java 2.x

    To get this service working with V2, setup your credentials in the .aws folder as described in the DEV Guide here:

    enter image description here

    See this topic:

    https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials.html

    No need to specify your creds in your code. Once you set the creds according to the docs, you can successfully use an ApiGatewayClient.

    This code example demonstrates how to obtain information about the current ApiKeys.

    //snippet-sourcedescription:[GetAPIKeys.java demonstrates how to obtain information about the current ApiKeys resource.]
    //snippet-keyword:[SDK for Java v2]
    //snippet-service:[Amazon API Gateway]
    /*
       Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
       SPDX-License-Identifier: Apache-2.0
    */
    
    package com.example.gateway;
    
    // snippet-start:[apigateway.java2.get_apikeys.import]
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.apigateway.ApiGatewayClient;
    import software.amazon.awssdk.services.apigateway.model.GetApiKeysResponse;
    import software.amazon.awssdk.services.apigateway.model.ApiKey;
    import software.amazon.awssdk.services.apigateway.model.ApiGatewayException;
    import java.util.List;
    // snippet-end:[apigateway.java2.get_apikeys.import]
    
    /**
     * To run this Java V2 code example, ensure that you have setup your development environment, including your credentials.
     *
     * For information, see this documentation topic:
     *
     * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
     */
    
    public class GetAPIKeys {
    
        public static void main(String[] args) {
    
            Region region = Region.US_EAST_1;
            ApiGatewayClient apiGateway = ApiGatewayClient.builder()
                    .region(region)
                    .build();
    
            getKeys(apiGateway);
            apiGateway.close();
        }
    
        // snippet-start:[apigateway.java2.get_apikeys.main]
        public static void getKeys(ApiGatewayClient apiGateway) {
    
            try {
                GetApiKeysResponse response = apiGateway.getApiKeys();
                List<ApiKey> keys = response.items();
                for (ApiKey key: keys) {
                    System.out.println("key id is: "+key.id());
                    System.out.println("key name is: "+key.name());
                }
    
            } catch (ApiGatewayException e) {
                System.err.println(e.awsErrorDetails().errorMessage());
                System.exit(1);
            }
        }
        // snippet-end:[apigateway.java2.get_apikeys.main]
    }
    

    The following screen shot shows this code working and the output.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search