简体   繁体   English

使用 AWS Amplify 和 Android 通过 Cognito 对 REST API 进行身份验证

[英]Authenticating a REST API with Cognito using AWS Amplify & Android

I am currently trying to configure a REST API I added using AWS Amplify.我目前正在尝试配置我使用 AWS Amplify 添加的 REST API。 I have already configured user authentication in which users can sign-up and sign-in by following the steps outlined in the authentication docs .我已经配置了用户身份验证,用户可以按照身份验证文档中概述的步骤注册和登录。 I then added a REST API using the api steps .然后我使用api 步骤添加了一个 REST API。

At the moment, I am just trying to retrieve a list of items from DynamoDB.目前,我只是想从 DynamoDB 中检索项目列表。 The api is successful when I test it on the aws console, however, when I make the call from my android api, it returns the following error:当我在 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]

I understand that amplify automatically sets the API to be restricted using AWS_IAM, which I think is why the above message is returned.我知道放大会使用 AWS_IAM 自动将 API 设置为受限,我认为这就是返回上述消息的原因。 I am trying to get it to authenticate using the user pools I setup before with the authentication steps.我正在尝试使用我之前通过身份验证步骤设置的用户池对其进行身份验证。 The code in my android that app that makes the call to the API is as follows:我的 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());
    }
});

Do I need to setup a Authorizer on AWS api console?我需要在 AWS api 控制台上设置授权方吗? And if so, How do I pass the authorization header with the user token.如果是这样,我如何使用用户令牌传递授权标头。 I have a seen a few responses from people using react native but not with android: AWS-amplify Including the cognito Authorization header in the request我看到一些使用 react native 但不使用 android 的人的回复: AWS-amplify 在请求中包含 cognito Authorization 标头

The function which the Api invokes is as follows if needed:如果需要,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);
    }
  });
});

Any points/help would be greatly appreciated!任何要点/帮助将不胜感激! Thanks!谢谢!

Have figured it out.已经想通了。 Even though Amplify is meant to take the credentials automatically when making an API call, it seemed to throw up the unauthorized error anyway.尽管 Amplify 旨在在进行 API 调用时自动获取凭据,但无论如何它似乎都会抛出未经授权的错误。 When I tested using the console it worked fine.当我使用控制台进行测试时,它工作正常。 I had to manually add the authorization header to the Rest options:我必须手动将授权标头添加到 Rest 选项:

RestOptions options = RestOptions.builder()
                .addPath("models")
                .addHeader("Authorization", token.getTokenString())
                .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM