Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用Cognito凭据调用和API网关端点的更简单方法,而无需生成SDK?_Java_Aws Api Gateway_Aws Cognito - Fatal编程技术网

在Java中使用Cognito凭据调用和API网关端点的更简单方法,而无需生成SDK?

在Java中使用Cognito凭据调用和API网关端点的更简单方法,而无需生成SDK?,java,aws-api-gateway,aws-cognito,Java,Aws Api Gateway,Aws Cognito,我不想为我的API网关端点使用生成的SDK。我想使用普通的AWS SDK。当我在谷歌上搜索如何做到这一点时,在最初的几次点击中没有任何明显的发现(令人惊讶)。所以我拼凑起来: private static void invokeApiGatewayWithCognito(String identityPoolId, String endpoint) throws URISyntaxException { AmazonCognitoIdentity cognitoClient = Amaz

我不想为我的API网关端点使用生成的SDK。我想使用普通的AWS SDK。当我在谷歌上搜索如何做到这一点时,在最初的几次点击中没有任何明显的发现(令人惊讶)。所以我拼凑起来:

private static void invokeApiGatewayWithCognito(String identityPoolId, String endpoint) throws URISyntaxException {
    AmazonCognitoIdentity cognitoClient = AmazonCognitoIdentityClient.builder()
            .withCredentials(new AWSCredentialsProvider() {
                @Override
                public AWSCredentials getCredentials() {
                    return new AnonymousAWSCredentials();
                }

                @Override
                public void refresh() {

                }
            }).withRegion(EU_CENTRAL_1.getName()).build();

    String identityId = cognitoClient.getId(new GetIdRequest()
            .withIdentityPoolId(identityPoolId))
            .getIdentityId();

    Credentials cognitoCreds = cognitoClient
            .getCredentialsForIdentity(
                    new GetCredentialsForIdentityRequest()
                            .withIdentityId(identityId))
            .getCredentials();

    AWS4Signer signer = new AWS4Signer();
    signer.setServiceName("execute-api");
    signer.setRegionName(EU_CENTRAL_1.getName());

    AmazonHttpClient amazonHttpClient =
            new AmazonHttpClient(new ClientConfiguration());
    Request r = new DefaultRequest("execute-api");
    r.setEndpoint(new URI(endpoint));
    r.setHttpMethod(HttpMethodName.GET);
    BasicSessionCredentials basicCreds = new BasicSessionCredentials(
            cognitoCreds.getAccessKeyId(),
            cognitoCreds.getSecretKey(),
            cognitoCreds.getSessionToken());

    signer.sign(r, basicCreds);

    amazonHttpClient
            .requestExecutionBuilder()
            .request(r)
            .execute(logResponse());
}

private static HttpResponseHandler<Void> logResponse() {
    return new HttpResponseHandler<Void>() {
        @Override
        public Void handle(HttpResponse httpResponse) throws Exception {
            System.out.println("response code = " + httpResponse.getStatusCode());
            System.out.println("response headers = " + httpResponse.getHeaders());
            System.out.println("response content = " + new String(bytes(httpResponse.getContent())));
            return null;
        }

        @Override
        public boolean needsConnectionLeftOpen() {
            return false;
        }
    };
}
private static void invokeApGatewayWithCognito(字符串标识工具ID,字符串端点)抛出URI语法异常{
AmazonCongnitoIdentity cognitoClient=AmazonCongnitoIdentityClient.builder()
.具有凭据(新AWSCredentialsProvider(){
@凌驾
公共AWSCredentials getCredentials(){
返回新的匿名AWSCredentials();
}
@凌驾
公共无效刷新(){
}
}).withRegion(EU_CENTRAL_1.getName()).build();
String identityId=cognitoClient.getId(新的GetIdRequest()
.withIdentityPoolId(identityPoolId))
.getIdentityId();
凭证cognitoCreds=cognitoClient
.getCredentialsForIdentity(
新GetCredentialsForIdentityRequest()
.使用identityId(identityId))
.getCredentials();
AWS4Signer signer=新的AWS4Signer();
signer.setServiceName(“执行api”);
signer.setRegionName(EU_CENTRAL_1.getName());
AmazonHttpClient AmazonHttpClient=
新的AmazonHttpClient(新的ClientConfiguration());
请求r=新的DefaultRequest(“执行api”);
r、 setEndpoint(新URI(端点));
r、 setHttpMethod(HttpMethodName.GET);
BasicSessionCredentials BasicCredentials=新的BasicSessionCredentials(
cognitoCreds.getAccessKeyId(),
cognitoCreds.getSecretKey(),
getSessionToken());
签名者。签名者(r,基本红色);
amazonHttpClient
.requestExecutionBuilder()
.请求(r)
.execute(logResponse());
}
专用静态HttpResponseHandler logResponse(){
返回新的HttpResponseHandler(){
@凌驾
公共无效句柄(HttpResponse HttpResponse)引发异常{
System.out.println(“响应代码=“+httpResponse.getStatusCode());
System.out.println(“response headers=“+httpResponse.getHeaders());
System.out.println(“response content=“+新字符串(字节(httpResponse.getContent())));
返回null;
}
@凌驾
公共布尔需求ConnectionLeftOpen(){
返回false;
}
};
}
它是有效的,但男孩就是那个丑陋的密码。我错过了什么?如何才能更干净地做到这一点