API调用403错误,可能是身份验证问题?

时间:2019-04-02 22:47:29

标签: amazon-web-services aws-lambda amazon-cognito

我想哭,我真的想哭。我这个问题已经三天了。我在aws lambda中有一个POST API,当我在aws lambda或cli中对其进行测试时,它可以正常工作。但不是从客户端调用API时。我收到以下错误POST https://sdigg5u4xb.execute-api.eu-west-1.amazonaws.com/prod/sites 403Uncaught (in promise) Error: Request failed with status code 403 at createError (createError.js:17) at settle (settle.js:19) at XMLHttpRequest.handleLoad (xhr.js:78)

起初,我认为这与CORS有关,但事实并非如此。由于这是403错误,因此必须与权限有关吗? 我曾尝试为aws用户赋予不同的策略和角色以有权执行所有操作,但我无法使其正常运行。在我哭泣之前,请帮助我。如果有人需要我的任何aws信息,那就问

Lambda代码:https://pastebin.com/vWNUQH0T

调用API:

从“ aws-amplify”导入{API};

export default (async function submitSite(values) {
  console.log(values);
  return API.post("sites", "/sites", {
    body: values
  })
});

在我的index.js中放大配置

Amplify.configure({
  Auth: {
    mandatorySignIn: true,
    region: config.cognito.REGION,
    userPoolId: config.cognito.USER_POOL_ID,
    identityPoolId: config.cognito.IDENTITY_POOL_ID,
    userPoolWebClientId: config.cognito.APP_CLIENT_ID
  },
  Storage: {
    region: config.s3.REGION,
    bucket: config.s3.BUCKET,
    identityPoolId: config.cognito.IDENTITY_POOL_ID
  },
  API: {
    endpoints: [
      {
        name: "sites",
        endpoint: config.apiGateway.URL,
        region: config.apiGateway.REGION
      },
    ]
  }
});

serverless.yml

functions:
  # Defines an HTTP API endpoint that calls the main function in create.js
  # - path: url path is /notes
  # - method: POST request
  # - cors: enabled CORS (Cross-Origin Resource Sharing) for browser cross
  #     domain api call
  # - authorizer: authenticate using the AWS IAM role
  createSite:
    handler: CreateSite.main
    events:
      - http:
          path: sites
          method: post
          cors: true
          authorizer: aws_iam

1 个答案:

答案 0 :(得分:0)

似乎您没有在发送POST请求时发送ID令牌。

尝试将其添加到您的通话中:

{
    headers: {
       Authorization: cognitoUser.getIdToken().getJwtToken()
    },
    body: values
}

cognitoUser将是您分配给用户的CognitoUser的任何对象名称。