使用AWS for Java V1初始化Amazon API网关客户端不工作

svmlkihl  于 2023-03-11  发布在  Java
关注(0)|答案(2)|浏览(125)

我有问题初始化API网关客户端在Java使用AWS-Java-SDK(尝试旧版本和最新版本)。我已经检查了10次,如果我的访问和密钥是正确的,他们是。我不知道我的错误编码,所以它停止工作后,“良好2”输出。需要帮助,请
下面是我的代码:

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

nbnkbykc

nbnkbykc1#

您正在使用旧的Java V1 API。AWS SDK Java团队不再建议您使用此API。您应考虑迁移到AWS SDK for Java V2。您可以在此处阅读有关此SDK版本的信息:
Developer guide - AWS SDK for Java 2.x
要使此服务与V2一起工作,请在.aws文件夹中设置您的凭据,如下面的DEV指南中所述:

请参阅此主题:
https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials.html
无需在代码中指定凭据。根据文档设置凭据后,即可成功使用ApiGatewayClient
此代码示例演示如何获取有关当前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]
}

下面的屏幕截图显示了这段代码的工作情况和输出。

sbtkgmzw

sbtkgmzw2#

安装了AWS SDK for Java 2 by Maven并添加了以下代码,但我的Burp扩展仍然无法正常工作:

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();
       
        
        
    }

结果是:

相关问题