The Authorization Code flow involves several steps to exchange an authorization code for an access token.
1. Initialize Authorization Request
Start by sending a GET request to the /oauth/authorize endpoint. This will prompt the resource owner to authenticate (via a login form) and approve or deny the client's access request.
Example URL:
http://localhost:8000/oauth/authorize?client_id=test_client&redirect_uri=https%3A%2F%2Fwww.example.com%2Fredirect&response_type=code&state=somestate&scope=email
2. Handle the Redirect
- On Success: The server redirects to the
redirect_uri with a code and the original state.
https://www.example.com/redirect?state=somestate&code=AUTHORIZATION_CODE&scope=email - On Error/Denial: The server redirects with an
error parameter.
https://www.example.com/redirect?error=access_denied&state=somestate
3. Exchange Code for Access Token
Use the /oauth/tokenize endpoint to exchange the code for tokens using Basic Authentication for the client credentials.
curl localhost:8000/oauth/tokenize \
-u 'test_client:password' \
-d 'grant_type=authorization_code' \
-d 'code=AUTHORIZATION_CODE' \
-d 'redirect_uri=https://www.example.com/redirect'
4. Refresh the Access Token
When the access token expires, use the refresh_token grant type at the /oauth/tokenize endpoint to obtain a new set of tokens.
curl localhost:8000/oauth/tokenize \
-u 'test_client:password' \
-d 'grant_type=refresh_token' \
-d 'refresh_token=REFRESH_TOKEN'
# Exchange code for token
curl localhost:8000/oauth/tokenize \
-u 'test_client:password' \
-d 'grant_type=authorization_code' \
-d 'code=EJKOGQhY7KcWjNGI2UbCnOrqAGtRiCEJnAYNwYJ8M5'\
-d 'redirect_uri=https://www.example.com/redirect'