如何通过 Android 上的改造 2 使用 Cognito Credentials 调用 API 网关? [英] How to call API Gateway with Cognito Credentials through retrofit2 on Android?

查看:23
本文介绍了如何通过 Android 上的改造 2 使用 Cognito Credentials 调用 API 网关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 android 应用程序中使用 retrofit2 进行任何 http/rest 调用.现在我需要调用一个由 Amazon AWS API Gateway 生成的 API.

I use retrofit2 in my android apps for any http/rest call. Now I need to call an api generated with Amazon AWS API Gateway.

AWS 文档 say 我应该生成客户端代码并抛出 API Gateway 控制台并使用 ApiClientFactory 类来构建请求:

The AWS documentation say I should generate the client code throw the API Gateway console and use the class ApiClientFactory to build the request:

ApiClientFactory factory = new ApiClientFactory();

// Use CognitoCachingCredentialsProvider to provide AWS credentials
// for the ApiClientFactory
AWSCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
        context,          // activity context
        "identityPoolId", // Cognito identity pool id
        Regions.US_EAST_1 // region of Cognito identity pool
};

factory.credentialsProvider(credentialsProvider);

// Create an instance of your SDK (this should come from the generated code).
final MyApiClient client = factory.build(MyApiClient.class);

// Invoke a method (e.g., 'parentPath1Get(param1,body)') exposed by your SDK. 
// Here the method's return type is OriginalModel.
OriginalModel output = client.parentPath1Get(param1,body);

// You also have access to your API's models.
OriginalModel myModel = new OriginalModel();
myModel.setStreetAddress(streetAddress);
myModel.setCity(city);
myModel.setState(state);
myModel.setStreetNumber(streetNumber);
myModel.setNested(nested);
myModel.setPoBox(poBox);

相反,我想像使用 retrofit 一样定义 API:使用我编写的接口,将其连接到 RxJava、OkHttp 等...

Instead I would like to define the API like I would with retrofit: with an interface I write, connect it to RxJava, OkHttp etc...

我的问题是:如何使用 Cognito Identity Provider 签署改造请求?

My question is: how can I sign the retrofit requests with Cognito Identity Provider?

推荐答案

基于@thanhbinh84 的回答创建了一个 OkHttp 拦截器.试试看:https://github.com/Ghedeon/AwsInterceptor

Created an OkHttp interceptor based on @thanhbinh84 answer. Give it a try: https://github.com/Ghedeon/AwsInterceptor

这篇关于如何通过 Android 上的改造 2 使用 Cognito Credentials 调用 API 网关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆