0

我有一个调用 API Gateway 端点的 AWS Lambda 函数。API 使用 API 密钥。函数和 API 都是 C#/.NET Core 3.1 API 启用了 CORS,并且 API 中的路由按预期工作。我可以从邮递员那里调用这条新路线,它工作正常。

第一次发布 Lambda 函数时,我可以调用它并且它可以成功运行。但是,每次后续调用都会导致 403。

谁能阐明我在这里做错了什么?

这是我从函数调用中得到的输出:

START RequestId: <redacted> Version: $LATEST
Execution started for function: <redacted> at 3/26/2021 4:37:03 AM
Input string is: {}
StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Connection: keep-alive
  Date: Fri, 26 Mar 2021 04:37:03 GMT
  x-amzn-RequestId: <redacted>
  Access-Control-Allow-Origin: *
  Access-Control-Allow-Headers: Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token
  x-amzn-ErrorType: ForbiddenException
  x-amz-apigw-id: <redacted>
  Access-Control-Allow-Methods: DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT
  X-Cache: Error from cloudfront
  Via: 1.1 <redacted>.cloudfront.net (CloudFront)
  X-Amz-Cf-Pop: SYD1-C2
  X-Amz-Cf-Id: <redacted>
  Content-Type: application/json
  Content-Length: 23
}
Response status code does not indicate success: 403 (Forbidden).: HttpRequestException
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at AWSLambda1.Function.FunctionHandler(String input, ILambdaContext context) in <redacted>\Function.cs:line 40
   at lambda_method(Closure , Stream , Stream , LambdaContextInternal )


END RequestId: <redacted>
REPORT RequestId: <redacted>    Duration: 263.07 ms Billed Duration: 264 ms Memory Size: 256 MB Max Memory Used: 87 MB

调用 API 的代码:

_client.DefaultRequestHeaders.Accept.Clear();
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _client.DefaultRequestHeaders.Add("x-api-key", _apiKey);

            var response = await _client.PostAsync(_apiRoute, new StringContent("{}", Encoding.UTF8, "application/json"));
            Console.WriteLine(response);
            response.EnsureSuccessStatusCode();
            
            var responseBody = await response.Content.ReadAsStringAsync();
4

0 回答 0