Skip to content

Reference - Integrations - FastAPI

OAuth2AuthorizeCallback

Dependency callable to handle the authorization callback. It reads the query parameters and returns the access token and the state.

Examples:

from fastapi import FastAPI, Depends
from httpx_oauth.integrations.fastapi import OAuth2AuthorizeCallback
from httpx_oauth.oauth2 import OAuth2

client = OAuth2("CLIENT_ID", "CLIENT_SECRET", "AUTHORIZE_ENDPOINT", "ACCESS_TOKEN_ENDPOINT")
oauth2_authorize_callback = OAuth2AuthorizeCallback(client, "oauth-callback")
app = FastAPI()

@app.get("/oauth-callback", name="oauth-callback")
async def oauth_callback(access_token_state=Depends(oauth2_authorize_callback)):
    token, state = access_token_state
    # Do something useful

__init__(client, route_name=None, redirect_url=None)

Parameters:

Name Type Description Default
client BaseOAuth2

An OAuth2 client.

required
route_name Optional[str]

Name of the callback route, as defined in the name parameter of the route decorator.

None
redirect_url Optional[str]

Full URL to the callback route.

None

OAuth2AuthorizeCallbackError

Bases: HTTPException, OAuth2Error

Error raised when an error occurs during the OAuth2 authorization callback.

It inherits from HTTPException, so you can either keep the default FastAPI error handling or implement a dedicated exception handler.