Amazon Cognito /oauth2/token returns unauthorized_client
{"error":"unauthorized_client"}Short answer
Cognito returns unauthorized_client from /oauth2/token when the app client is not allowed to use the grant you asked for, or when the redirect_uri in the token request differs from the one used at /oauth2/authorize. Read error_description first; invalid_redirect points to the URI, anything else points to the app client's allowed flows.
Fix it fast
1Find out which cause you have
Repeat the token request with curl and read the whole JSON body, including error_description. Use the same redirect_uri you sent to /oauth2/authorize.
curl -s -i -X POST "https://my-domain.auth.us-east-1.amazoncognito.com/oauth2/token" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "grant_type=authorization_code" \ -d "client_id=1example23456789" \ -d "code=AUTHORIZATION_CODE" \ -d "redirect_uri=https://app.example.com/callback"2Match what you see, then jump to its fix
- 1
"error":"unauthorized_client" with "error_description":"invalid_redirect"redirect_uri in the token request is not the one used at /oauth2/authorizeFix - 2
"error":"unauthorized_client" with no invalid_redirect descriptionThe app client is not allowed to use the authorization code grant or to refresh tokensFix - 3
grant_type=client_credentials returns an unauthorized-class errorYou request client_credentials from an app client that cannot use itFix
- 1
What does the error look like?
{"error":"unauthorized_client"}{"error":"unauthorized_client","error_description":"invalid_redirect"}HTTP/1.1 400 Bad Request from POST /oauth2/token
Cause 1 of 3
redirect_uri in the token request is not the one used at /oauth2/authorize
1Check
For the authorization_code grant the token request must carry exactly the redirect_uri that started the sign-in. A trailing slash, a different port or http against https is enough to fail.
# Compare redirect_uri in the /oauth2/authorize URL with redirect_uri in the POST body2Fix
Send the identical string in both places, and make sure it is also in the app client's allowed callback URLs.
redirect_uri=https://app.example.com/callback3Verify
The token endpoint answers 200 with access_token, id_token and refresh_token.
# Expect HTTP/1.1 200 OK and JSON with access_tokenConfirmed by docs.aws.amazon.com
Cause 2 of 3
The app client is not allowed to use the authorization code grant or to refresh tokens
1Check
AWS lists unauthorized_client for a client that is not allowed for the code grant flow or for refreshing tokens. Read the app client's allowed OAuth flows.
aws cognito-idp describe-user-pool-client --user-pool-id us-east-1_EXAMPLE \
--client-id 1example23456789 \
--query 'UserPoolClient.[AllowedOAuthFlows,AllowedOAuthFlowsUserPoolClient,AllowedOAuthScopes,CallbackURLs]'2Fix
Enable the grant you use on the app client (authorization code grant for user sign-in) and set the callback URLs. update-user-pool-client replaces unspecified settings, so repeat the existing values you want to keep.
aws cognito-idp update-user-pool-client --user-pool-id us-east-1_EXAMPLE \
--client-id 1example23456789 \
--allowed-o-auth-flows code \
--allowed-o-auth-flows-user-pool-client \
--allowed-o-auth-scopes openid email \
--callback-urls https://app.example.com/callback \
--supported-identity-providers COGNITO3Verify
Run the token request again; it returns tokens.
# Repeat the curl from the first step with a fresh codeConfirmed by docs.aws.amazon.com
Cause 3 of 3
You request client_credentials from an app client that cannot use it
1Check
AWS says the client credentials grant needs scopes enabled on the app client, a client secret, and the client_credentials flow enabled (AllowedOAuthFlows for a hosted domain). With none of these, the endpoint returns an unauthorized-class error. The command shows flows and scopes; confirm in the console that the app client has a secret (do not print it).
aws cognito-idp describe-user-pool-client --user-pool-id us-east-1_EXAMPLE \
--client-id 1example23456789 \
--query 'UserPoolClient.[AllowedOAuthFlows,AllowedOAuthScopes]'2Fix
Use a separate app client with a secret that allows only client_credentials and a custom scope from a resource server; AWS says this grant cannot be combined with the code or implicit grants on one client, and a secret cannot be added to an existing client. Send the secret with HTTP Basic auth.
curl -s -X POST "https://my-domain.auth.us-east-1.amazoncognito.com/oauth2/token" \
-u "1example23456789:CLIENT_SECRET" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "scope=my-api/read"3Verify
The response contains an access_token and no refresh token.
# Expect JSON with access_token, token_type Bearer, expires_inConfirmed by docs.aws.amazon.com
How do I stop it coming back?
- Keep the callback URL in one config value used for both the authorize redirect and the token call.
- Use an OIDC library for the token exchange so redirect_uri and PKCE values are carried through automatically.
- Keep a separate app client per use: one public client with PKCE for sign-in, one confidential client for machine-to-machine.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- RaheebA consumer app shipped to both stores from one Flutter codebase on a .NET and Azure back end.
- Loade DriverThe driver side of a delivery platform: live jobs, routing and earnings, past 5K Google Play downloads.
Related fixes
Security
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
Angular HttpClient does not add the X-XSRF-TOKEN header to a request
403 Forbidden on POST, PUT or DELETE while GET works
Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script.
5 causes, each with a fixSecurity
An [Authorize] endpoint rejects a request with a bearer token
Authorization Header sent: no Bearer token sent.
An [Authorize] endpoint without a policy name requires an authenticated user.
4 causes, each with a fixSecurity
ASP.NET Core API rejects an Angular user's token: the signature key was not found
401 Unauthorized
The API looked for the key named by the token's kid in the key set it downloaded from your Authority, and the key was not there.
4 causes, each with a fixSecurity
ASP.NET Core rejects the token before checking it: IDX12741, three segments expected
401 Unauthorized
IDX12741 means the string the API pulled out of the Authorization header is not shaped like a JWT: a signed token has three dot-separated parts and an encrypted one has five.
3 causes, each with a fixSecurity
A Microsoft Entra access token shows Invalid Signature when you check it yourself
Tokens that a Microsoft API receives might not always be a JWT that can be decoded
A token issued for Microsoft Graph is not meant for you to validate, so a failed signature check is expected.
2 causes, each with a fix