gpt4 book ai didi

Android AWS APIGateway 身份验证 HttpURLConnection

转载 作者:行者123 更新时间:2023-11-29 19:46:15 25 4
gpt4 key购买 nike

在使用 Cognito 和 API Gateway 访问我们的 REST API 时遇到问题

我这辈子都找不到一个简单的例子来说明如何在我们的 AWS 服务器上调用安全的 REST API。

我打电话

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
ProfileViewActivity.this, // get the context for the current activity
AMAZON_COGNITO_IDENTITY_POOL_ID, // your identity pool id
Regions.US_EAST_1 //Region
);

然后尝试调用属于该 POOL 的我们的服务器。我一直回到“禁止”。

我应该如何处理 HttpURLConnection header 中的 credentialsProvider?

更新:

可以通过 AWS 生成的 SDK 以正确的方式使用 APIGateWay,但不确定如何更改生成的文件:

    private class MyAsyncTask extends AsyncTask<String, Void, String> {
private TextView textView;

public MyAsyncTask() {
}

@Override
protected String doInBackground(String... strings) {
AWSCredentialsProvider credenetialsProvider = new CognitoCachingCredentialsProvider(
ProfileViewActivity.this, // activity context
AMAZON_COGNITO_IDENTITY_POOL_ID, // Cognito identity pool id
Regions.US_EAST_1 // region of Cognito identity pool
);

ApiClientFactory factory = new ApiClientFactory()
.credentialsProvider(credenetialsProvider)
.region("us-east-1")
.endpoint("https://myendpoint")
.apiKey("xxxxxxxxxxxxxxxxxxxxxxx");


// MyClient is the AWS Android SDK Generated class

final MyClient client = factory.build(MyClient.class);

client.feedGet();

String str = client.testGet().toString();


Log.d("###", "here after test" +client.testGet().toString());


return "DONE";
}

@Override
protected void onPostExecute(String temp) {
Log.d("####", "onPostExecute");
}
}

然后

    private class MyAsyncTask extends AsyncTask<String, Void, String> {
private TextView textView;

public MyAsyncTask() {
}

@Override
protected String doInBackground(String... strings) {
AWSCredentialsProvider credenetialsProvider = new CognitoCachingCredentialsProvider(
ProfileViewActivity.this, // activity context
AMAZON_COGNITO_IDENTITY_POOL_ID, // Cognito identity pool id
Regions.US_EAST_1 // region of Cognito identity pool
);

ApiClientFactory factory = new ApiClientFactory()
.credentialsProvider(credenetialsProvider)
.region("us-east-1")
.endpoint("https://myendpoint")
.apiKey("xxxxxxxxxxxxxxxxxxxxxxx");


// MyClient is the AWS Android SDK Generated class

final MyClient client = factory.build(MyClient.class);

client.feedGet();

String str = client.testGet().toString();


Log.d("###", "here after test" +client.testGet().toString());


return "DONE";
}

@Override
protected void onPostExecute(String temp) {
Log.d("####", "onPostExecute");
}
}

========= MyClient.java - AWS Android SDK 生成的文件

    @com.amazonaws.mobileconnectors.apigateway.annotation.Service(endpoint = https:myaws_server)
public interface MyClient {
@com.amazonaws.mobileconnectors.apigateway.annotation.Operation(path = \"/test\", method = "GET")
Empty testGet();

最佳答案

如果您使用的是 AWS API Gateway,则可以在不使用 AWS Cognito 的情况下独立使用它。 AWS API Gateway 是一种 http 服务,就像您服务器的代理或 AWS Lambda 函数的网关。

aws api gateway

关于Android AWS APIGateway 身份验证 HttpURLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37597549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com