TOKEN Endpoint
The /oauth2/token endpoint gets the user's tokens.
POST /oauth2/token
The /oauth2/token endpoint only supports HTTPS POST. The user pool
client makes requests to this endpoint directly and not through the system
browser.
Request Parameters in Headers
- Authorization
-
If the client was issued a secret, the client must pass its
client_idandclient_secretin the authorization header through Basic HTTP authorization. The secret is BasicBase64Encode(client_id:client_secret). - Content-Type
-
Must always be
'application/x-www-form-urlencoded'.
Request Parameters in Body
- grant_type
-
Grant type.
Must be
authorization_codeorrefresh_tokenorclient_credentials.Required
- client_id
-
Client ID.
Must be a preregistered client in the user pool. The client must be enabled for Amazon Cognito federation.
Required if the client is public and does not have a secret.
- scope
-
Can be a combination of any custom scopes associated with a client. Any scope requested must be preassociated with the client or it will be ignored at runtime. If the client doesn't request any scopes, the authentication server uses all custom scopes associated with the client.
Optional. Only used if the
grant_typeisclient_credentials. - redirect_uri
-
Must be the same
redirect_urithat was used to getauthorization_codein /oauth2/authorize.Required only if
grant_typeisauthorization_code. - refresh_token
-
The refresh token.
Required if
grant_typeisrefresh_token. - code
-
Required if
grant_typeisauthorization_code. - code_verifier
-
The proof key.
Required if
grant_typeisauthorization_codeand the authorization code was requested with PKCE.
Examples of Positive Requests
Exchanging an Authorization Code for Tokens
Sample Request
POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token& Content-Type='application/x-www-form-urlencoded'& Authorization=Basic aSdxd892iujendek328uedj grant_type=authorization_code& client_id=djc98u3jiedmi283eu928& code=AUTHORIZATION_CODE& redirect_uri=com.myclientapp://myclient/redirect
Sample response
HTTP/1.1 200 OK Content-Type: application/json { "access_token":"eyJz9sdfsdfsdfsd", "refresh_token":"dn43ud8uj32nk2je", "id_token":"dmcxd329ujdmkemkd349r", "token_type":"Bearer", "expires_in":3600 }
Exchanging Client Credentials for an Access Token
Sample Request
POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token > Content-Type='application/x-www-form-urlencoded'& Authorization=Basic aSdxd892iujendek328uedj grant_type=client_credentials& scope={resourceServerIdentifier1}/{scope1} {resourceServerIdentifier2}/{scope2}
Sample response
HTTP/1.1 200 OK Content-Type: application/json { "access_token":"eyJz9sdfsdfsdfsd", "token_type":"Bearer", "expires_in":3600 }
Exchanging an Authorization Code Grant with PKCE for Tokens
Sample Request
POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token Content-Type='application/x-www-form-urlencoded'& Authorization=Basic aSdxd892iujendek328uedj grant_type=authorization_code& client_id=djc98u3jiedmi283eu928& code=AUTHORIZATION_CODE& code_verifier=CODE_VERIFIER& redirect_uri=com.myclientapp://myclient/redirect
Sample response
HTTP/1.1 200 OK Content-Type: application/json { "access_token":"eyJz9sdfsdfsdfsd", "refresh_token":"dn43ud8uj32nk2je", "id_token":"dmcxd329ujdmkemkd349r", "token_type":"Bearer", "expires_in":3600 }
Exchanging a Refresh Token for Tokens
Sample Request
POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token > Content-Type='application/x-www-form-urlencoded' Authorization=Basic aSdxd892iujendek328uedj grant_type=refresh_token& client_id=djc98u3jiedmi283eu928& refresh_token=REFRESH_TOKEN
Sample Response
HTTP/1.1 200 OK Content-Type: application/json { "access_token":"eyJz9sdfsdfsdfsd", "refresh_token":"dn43ud8uj32nk2je", "id_token":"dmcxd329ujdmkemkd349r", "token_type":"Bearer", "expires_in":3600 }
Examples of Negative Requests
Sample Error Response
HTTP/1.1 400 Bad Request Content-Type: application/json;charset=UTF-8 { "error":"invalid_request|invalid_client|invalid_grant|unauthorized_client|unsupported_grant_type|" }
- invalid_request
-
The request is missing a required parameter, includes an unsupported parameter value (other than
unsupported_grant_type), or is otherwise malformed. For example,grant_typeisrefresh_tokenbutrefresh_tokenis not included. - invalid_client
-
Client authentication failed. For example, when the client includes
client_idandclient_secretin the authorization header, but there's no such client with thatclient_idandclient_secret. - invalid_grant
-
Refresh token has been revoked.
Authorization code has been consumed already or does not exist.
- unauthorized_client
-
Client is not allowed for code grant flow or for refreshing tokens.
- unsupported_grant_type
-
Returned if
grant_typeis anything other thanauthorization_codeorrefresh_token.




