@oauthtoken

Overview

The @oauthtoken decorator marks a function as handling OAuth tokens, enabling the function to be automatically recognized as part of an OAuth workflow.

Syntax

@oauthtoken
def function_name(self, access_token: str, expires_in: Optional[int] = None) -> None:

Note: This decorator always applies to the same method signature, but the method itself can have any name.

Example

@oauth(
    id="oauth",
    provider=OAuthProvider.MICROSOFT,
    flows=[OAuthFlow.AUTHORIZATION_CODE],
    authorize_endpoint="https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/authorize",
    token_endpoint="https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token",
    scopes=[
        "https://graph.microsoft.com/Files.ReadWrite",
        "https://graph.microsoft.com/Sites.ReadWrite.All",
    ],
)
@book(name="MicrosoftBook")
class BaseMicrosoftBook:
    """
    Base class for all Microsoft Books.

    OAuth Arguments:
        TENANT_ID: The tenant ID of the Azure AD directory.

    OAuth Labels:
        TENANT_ID: Tenant ID
    """

    _acquired_token: Optional[AcquiredToken] = None
    _headers: Optional[dict] = None

    def __init__(self):
        """
        Initializes an instance of the class.
        """
        self._acquired_token = None
        self._headers = None

    @oauthtoken
    def handle_token(self, access_token: str, expires_in: Optional[int] = None) -> None:
        """
        Handles the OAuth token.

        Args:
            access_token (str): The OAuth access token.
            expires_in (Optional[int]): The expiration time of the token, in seconds.

        Labels:
            access_token: OAuth Access Token
            expires_in: Token Expiration Time
        """
        self._acquired_token = AcquiredToken(access_token, expires_in or 3600)

What’s Next