This tutorial will help you call your own API using the Authorization Code Flow. If you want to learn how the flow works and why you should use it, see Authorization Code Flow. If you want to learn to add login to your regular web app, see Add Login Using the Authorization Code Flow.
- Regular Web App Quickstarts: The easiest way to implement the flow.
- Authentication API: If you prefer to build your own solution, keep reading to learn how to call our API directly.
Prerequisites
Before beginning this tutorial:-
Register your Application with Auth0.
- Select an Application Type of Regular Web Apps.
- Add an Allowed Callback URL of
{https://yourApp/callback}
. - Make sure your Application’s Grant Types include Authorization Code. To learn how, read Update Grant Types.
- If you want your Application to be able to use refresh tokens, make sure the Application’s Grant Types include refresh token. To learn how, read Update Grant Types. To learn more about Refresh Tokens, read refresh tokens.
-
Register your API with Auth0
- If you want your API to receive refresh tokens to allow it to obtain new tokens when the previous ones expire, enable Allow Offline Access.
Steps
Authorize user
Authorize user
Request tokens
Request tokens
Now that you have an Authorization Code, you must exchange it for tokens. Using the extracted Authorization Code (
ID tokens contain user information that must be decoded and extracted.Access tokens are used to call the Auth0 Authentication API’s /userinfo endpoint or another API. If you are calling your own API, the first thing your API will need to do is verify the Access token.Refresh tokens are used to obtain a new access token or ID token after the previous one has expired. The
code
) from the previous step, you will need to POST
to the token URL.Example POST to token URL
Parameters
Parameter Name | Description |
---|---|
grant_type | Set this to authorization_code . |
code | The authorization_code retrieved in the previous step of this tutorial. |
client_id | Your application’s Client ID. You can find this value in your Application Settings. |
client_secret | Your application’s Client Secret. You can find this value in your Application Settings. To learn more about available application authentication methods, read Application Credentials. |
redirect_uri | The valid callback URL set in your Application settings. This must exactly match the redirect_uri passed to the authorization URL in the previous step of this tutorial. Note that this must be URL encoded. |
Response
If all goes well, you’ll receive anHTTP 200
response with a payload containing access_token
, refresh_token
, id_token
, and token_type
values:Validate your tokens before saving them. To learn how, read Validate ID Tokens and Validate Access Tokens.
refresh_token
will only be present in the response if you included the offline_access
scope and enabled Allow Offline Access for your API in the Dashboard.Refresh tokens must be stored securely since they allow a user to remain authenticated essentially forever.
Make an API call
Make an API call
To call your API from a regular web application, the application must pass the retrieved access token as a Bearer token in the Authorization header of your HTTP request.
Exchange refresh token
Exchange refresh token
You have already received a refresh token if you’ve been following this tutorial and completed the following:
- configured your API to allow offline access
- included the
offline_access
scope when you initiated the authentication request through the authorize endpoint.
POST
request to the /oauth/token
endpoint in the Authentication API, using grant_type=refresh_token
.Example POST to token URL
Parameters
Parameter Name | Description |
---|---|
grant_type | Set this to refresh_token . |
client_id | Your application’s Client ID. You can find this value in your Application Settings. |
refresh_token | The refresh token to use. |
scope | (optional) A space-delimited list of requested scope permissions. If not sent, the original scopes will be used; otherwise you can request a reduced set of scopes. Note that this must be URL encoded. |
Response
If all goes well, you’ll receive anHTTP 200
response with a payload containing a new access_token
, its lifetime in seconds (expires_in
), granted scope
values, and token_type
. If the scope of the initial token included openid
, then the response will also include a new id_token
:Validate your tokens before saving them. To learn how, read Validate ID Tokens and Validate Access Tokens.
Sample use cases
Customize tokens
You can use Auth0 Actions to modify the scopes of an and/or add custom claims to access and . To learn more about Actions, see Understand how Auth0 Actions Work. To do so, add the following Post-Login Action, which will run after the user authenticates:Auth0 returns profile information in a structured claim format as defined by the OpenID Connect (OIDC) specification. This means that custom claims added to ID tokens or access tokens must conform to guidelines and restrictions to avoid possible collisions.