我在我的android應用程序中使用retrofit2
進行任何http/rest調用。現在我需要調用一個由Amazon AWS API Gateway生成的api。如何通過Android上的retrofit2調用帶有Cognito憑證的API網關?
的AWS文檔say我應該生成客戶端代碼拋出API網關控制檯和使用類ApiClientFactory
建立請求:
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);
相反,我想定義的API就像我會retrofit
:與我寫的接口,將其連接到RxJava,OkHttp等...
我的問題是:如何使用Cognito Identity Provider簽署改造請求?
我最終直接使用了AWS SDK,並將其封裝在Rx Observables中。 Jack Kohn的回答並沒有錯,但它也不是真正的答案,如果你想自己實施它,它只是指向正確的方向。 –
所以我試圖掛鉤與API網關的android,但如何或在哪裏得到/創建類MyApiClient? – TheQ
這個名字取決於你的服務。 Theres是api網關的Web控制檯的生成器/導出。儘管我沒有使用這種方法。 –