![](/img/trans.png)
[英]Aws sam cognito api gateway - access token forbidden but works if it's from postman
[英]Api Gateway Cognito Authorizer: client token works on AWS ui but not on Postman
我使用無服務器框架設置了一些 POST api,然后編寫了一個原始的 NextJs 應用程序來嘗試使用 Cognito 進行身份驗證,然后使用受授權方保護的 api。
我也看過這篇文章 ( https://www.serverless.com/blog/cors-api-gateway-survival-guide/ ) 但沒有運氣,此時我不知道該怎么做。
我的 serverless.yml 的一部分:
custom:
allowedHeaders:
- Accept
- Content-Type
- Content-Length
- Authorization
- X-Amz-Date
- X-Api-Key
- X-Amz-Security-Token
- X-Amz-User-Agent
- app_user_id
- app_user_name
...
createResource:
handler: api/${name}.handler
description: POST /${name}
events:
- http:
path: api/${name}
method: post
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayCognitoAuthorizer
scopes:
- email
cors:
origin: '*'
headers: ${self:custom.allowedHeaders}
reqValidatorName: ${name}RequestValidator
integration: lambda
request:
template:
application/json: ${file(api/resource/mappingTemplates/${name}RequestMappingTemplate.vm)}
passThrough: WHEN_NO_MATCH
...
ApiGatewayCognitoAuthorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
Name: CognitoUserPool-${self:provider.environment.VERSION}
Type: COGNITO_USER_POOLS
IdentitySource: method.request.header.Authorization
RestApiId:
Ref: ApiGatewayRestApi
ProviderARNs:
- ${self:custom.settings.cognitoARN-${env:ENV_NAME}}
經過一系列嘗試,我解決了這個問題。 我發現這篇文章非常相似:stackoverflow.com/questions/46345005/… 對我有用的解決方案是將“openid”添加到 api 的 oauth 范圍,而不僅僅是“電子郵件”。 然后我在 Postman 和瀏覽器上嘗試了 access_token 而不是 id_token 和 hallelujah
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.