- Newest
- Most votes
- Most comments
Based on the information provided, it seems you're encountering some common issues when integrating Amazon Cognito with API Gateway. Let's address your questions and provide some guidance:
-
Custom scopes: Custom scopes are typically used with access tokens, not ID tokens. If you're using an access token, you need to ensure that the custom scopes you've defined in your Cognito user pool match the scopes you've configured on your API Gateway methods. However, it's not always necessary to use custom scopes if you're just looking for basic authentication.
-
Using ID token vs Access token: You can use either an ID token or an access token to invoke an API, but they serve different purposes:
- ID token: Used for authentication and contains claims about the user's identity.
- Access token: Used for authorization and contains scopes that define what actions the user can perform.
For simple authentication scenarios, using the ID token is often sufficient. If you're using the ID token, you don't need to configure custom scopes.
- Policy issues: If you're seeing the "User: anonymous is not authorized to perform: execute-api:Invoke" error, it suggests that the token is not being properly recognized or validated by API Gateway. This could be due to incorrect configuration of the Cognito User Pools authorizer in API Gateway.
To troubleshoot and resolve your issues:
-
In API Gateway, ensure your Cognito User Pools authorizer is correctly configured:
- Verify that the correct Cognito User Pool is selected.
- Check that the Token Source is set to "Authorization" (this is the header name Postman should use).
-
In Postman:
- Set the Authorization type to "Bearer Token".
- In the Token field, paste your ID token (not the access token).
- Ensure the token is fresh and not expired.
-
If you decide to use access tokens and custom scopes:
- In Cognito, create a resource server with the custom scopes you need.
- In API Gateway, for each method that requires authorization, specify the full scope names (e.g., https://your-api-id.execute-api.region.amazonaws.com/stage/scope-name) in the method's authorization settings.
-
Double-check your API's deployment:
- Make sure you've deployed your API after making any changes to the authorizer or method settings.
-
Verify your Cognito app client settings:
- Ensure that the app client used to obtain the token is configured to allow the grant types you're using (e.g., authorization code, client credentials).
If you're still encountering issues after trying these steps, you may need to review your API Gateway logs or CloudWatch logs for more detailed error messages that could point to the specific problem.
Sources
Configuring machine to machine Authentication with Amazon Cognito and Amazon API Gateway – Part 2 | AWS Cloud Operations Blog
Trouble get access to API GAteway with Cognito UserPool | AWS re:Post
Integrate a REST API with an Amazon Cognito user pool - Amazon API Gateway
Control access to REST APIs using Amazon Cognito user pools as an authorizer - Amazon API Gateway
Accessing resources with API Gateway after sign-in - Amazon Cognito
