【发布时间】:2020-06-01 10:44:03
【问题描述】:
我目前正在尝试配置使用 AWS Amplify 添加的 REST API。我已经配置了用户身份验证,用户可以按照authentication docs 中列出的步骤进行注册和登录。然后我使用api steps 添加了一个 REST API。
目前,我只是想从 DynamoDB 中检索项目列表。当我在 aws 控制台上测试它时,api 是成功的,但是,当我从我的 android api 进行调用时,它返回以下错误:
{"message":"Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=[a long string of characters]
我了解 amplify 会自动将 API 设置为使用 AWS_IAM 进行限制,我认为这就是返回上述消息的原因。我正在尝试使用我之前通过身份验证步骤设置的用户池对其进行身份验证。在我的 android 应用程序中调用 API 的代码如下:
RestOptions options = new RestOptions("/models");
Amplify.API.get("modelsapi", options, new ResultListener<RestResponse>() {
@Override
public void onResult(RestResponse restResponse) {
Log.i(TAG, restResponse.toString());
Log.i(TAG, restResponse.getData().asString());
}
@Override
public void onError(Throwable throwable) {
Log.e(TAG, throwable.toString());
}
});
我需要在 AWS api 控制台上设置授权人吗?如果是这样,我如何通过用户令牌传递授权标头。我看到一些使用 react native 但不使用 android 的人的回复:AWS-amplify Including the cognito Authorization header in the request
如果需要,Api调用的函数如下:
app.get(path, function(req, res) {
let queryParams = {
TableName: tableName
}
dynamodb.scan(queryParams, (err, data) => {
if (err) {
res.statusCode = 500;
res.json({error: 'Could not load items: ' + err});
} else {
res.json(data.Items);
}
});
});
任何积分/帮助将不胜感激!谢谢!
【问题讨论】:
-
更新:我重新配置了 API 以使用 Cognito 用户池授权器。我按照以下步骤操作:github.com/aws-amplify/amplify-cli/issues/…。但是现在错误消息只返回一个
{"message":"Unauthorized"}。
标签: android aws-api-gateway amazon-cognito aws-amplify aws-amplify-sdk-android