Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Amazon Cognito /oauth2/token returns unauthorized_client

Error{"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.

  • Applies to Amazon Cognito user pools with a domain (managed login), AWS CLI 2.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1"error":"unauthorized_client" with "error_description":"invalid_redirect"redirect_uri in the token request is not the one used at /oauth2/authorizeFix
    2. 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. 3grant_type=client_credentials returns an unauthorized-class errorYou request client_credentials from an app client that cannot use itFix

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 body

2Fix

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/callback

3Verify

The token endpoint answers 200 with access_token, id_token and refresh_token.

# Expect HTTP/1.1 200 OK and JSON with access_token

Confirmed by docs.aws.amazon.com

Did this fix it?

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 COGNITO

3Verify

Run the token request again; it returns tokens.

# Repeat the curl from the first step with a fresh code

Confirmed by docs.aws.amazon.com

Did this fix it?

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_in

Confirmed by docs.aws.amazon.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.