Skip to content

Google

google

Google OAuth provider implementation.

Provides Google OAuth 2.0 authentication using authorization code flow with PKCE support.

Classes

GoogleOAuthProvider

GoogleOAuthProvider(*, client_id: str, client_secret: str)

Bases: OAuthProvider

Google OAuth 2.0 provider implementation.

Uses Google's OAuth 2.0 endpoints for authorization code flow. Supports PKCE and refresh tokens.

Initialize Google OAuth provider.

Parameters:

Name Type Description Default
client_id str

Google OAuth client ID

required
client_secret str

Google OAuth client secret

required
Source code in fastauth/providers/google.py
def __init__(self, *, client_id: str, client_secret: str):
    """
    Initialize Google OAuth provider.

    Args:
        client_id: Google OAuth client ID
        client_secret: Google OAuth client secret
    """
    self.client_id = client_id
    self.client_secret = client_secret
Attributes
name property
name: str

Provider name.

authorization_endpoint property
authorization_endpoint: str

Google OAuth authorization URL.

token_endpoint property
token_endpoint: str

Google OAuth token exchange URL.

user_info_endpoint property
user_info_endpoint: str

Google user info API endpoint.

default_scopes property
default_scopes: str

Default OAuth scopes for Google (space-separated).

Functions
exchange_code_for_tokens async
exchange_code_for_tokens(
    *, code: str, redirect_uri: str, code_verifier: str | None = None
) -> OAuthTokens

Exchange authorization code for access/refresh tokens.

Parameters:

Name Type Description Default
code str

Authorization code from callback

required
redirect_uri str

Redirect URI used in authorization

required
code_verifier str | None

Optional PKCE code verifier

None

Returns:

Type Description
OAuthTokens

OAuthTokens with access token and optional refresh token

Raises:

Type Description
HTTPError

If token exchange fails

Source code in fastauth/providers/google.py
async def exchange_code_for_tokens(
    self,
    *,
    code: str,
    redirect_uri: str,
    code_verifier: str | None = None,
) -> OAuthTokens:
    """
    Exchange authorization code for access/refresh tokens.

    Args:
        code: Authorization code from callback
        redirect_uri: Redirect URI used in authorization
        code_verifier: Optional PKCE code verifier

    Returns:
        OAuthTokens with access token and optional refresh token

    Raises:
        httpx.HTTPError: If token exchange fails
    """
    data = {
        "code": code,
        "client_id": self.client_id,
        "client_secret": self.client_secret,
        "redirect_uri": redirect_uri,
        "grant_type": "authorization_code",
    }

    if code_verifier:
        data["code_verifier"] = code_verifier

    async with httpx.AsyncClient() as client:
        response = await client.post(self.token_endpoint, data=data)
        response.raise_for_status()
        token_data = response.json()

    return OAuthTokens(
        access_token=token_data["access_token"],
        refresh_token=token_data.get("refresh_token"),
        expires_in=token_data.get("expires_in"),
        token_type=token_data.get("token_type", "Bearer"),
    )
get_user_info async
get_user_info(*, access_token: str) -> OAuthUserInfo

Fetch user information using access token.

Parameters:

Name Type Description Default
access_token str

OAuth access token

required

Returns:

Type Description
OAuthUserInfo

OAuthUserInfo with user profile data

Raises:

Type Description
HTTPError

If user info fetch fails

Source code in fastauth/providers/google.py
async def get_user_info(self, *, access_token: str) -> OAuthUserInfo:
    """
    Fetch user information using access token.

    Args:
        access_token: OAuth access token

    Returns:
        OAuthUserInfo with user profile data

    Raises:
        httpx.HTTPError: If user info fetch fails
    """
    headers = {"Authorization": f"Bearer {access_token}"}

    async with httpx.AsyncClient() as client:
        response = await client.get(self.user_info_endpoint, headers=headers)
        response.raise_for_status()
        user_data = response.json()

    return OAuthUserInfo(
        provider_user_id=user_data["id"],
        email=user_data["email"],
        email_verified=user_data.get("verified_email", False),
        name=user_data.get("name"),
        avatar_url=user_data.get("picture"),
    )
refresh_access_token async
refresh_access_token(*, refresh_token: str) -> OAuthTokens

Refresh access token using refresh token.

Parameters:

Name Type Description Default
refresh_token str

OAuth refresh token

required

Returns:

Type Description
OAuthTokens

OAuthTokens with new access token

Raises:

Type Description
HTTPError

If token refresh fails

Source code in fastauth/providers/google.py
async def refresh_access_token(self, *, refresh_token: str) -> OAuthTokens:
    """
    Refresh access token using refresh token.

    Args:
        refresh_token: OAuth refresh token

    Returns:
        OAuthTokens with new access token

    Raises:
        httpx.HTTPError: If token refresh fails
    """
    data = {
        "refresh_token": refresh_token,
        "client_id": self.client_id,
        "client_secret": self.client_secret,
        "grant_type": "refresh_token",
    }

    async with httpx.AsyncClient() as client:
        response = await client.post(self.token_endpoint, data=data)
        response.raise_for_status()
        token_data = response.json()

    return OAuthTokens(
        access_token=token_data["access_token"],
        refresh_token=token_data.get("refresh_token"),
        expires_in=token_data.get("expires_in"),
        token_type=token_data.get("token_type", "Bearer"),
    )