OAuth

class ytmusicapi.auth.oauth.OAuthCredentials(client_id: str, client_secret: str, session: Session | None = None, proxies: dict | None = None)

Bases: Credentials

Class for handling OAuth credential retrieval and refreshing.

client_id: str
client_secret: str
get_code() AuthCodeDict

Method for obtaining a new user auth code. First step of token creation.

Return type:

AuthCodeDict

refresh_token(refresh_token: str) BaseTokenDict

Method for requesting a new access token for a given refresh_token. Token must have been created by the same OAuth client.

Parameters:

refresh_token (str) – Corresponding refresh_token for a matching access_token. Obtained via

Return type:

BaseTokenDict

token_from_code(device_code: str) RefreshableTokenDict

Method for verifying user auth code and conversion into a FullTokenDict.

Return type:

RefreshableTokenDict

class ytmusicapi.auth.oauth.OAuthToken(scope: str | Literal['https://www.googleapis.com/auth/youtube'], token_type: str | Literal['Bearer'], access_token: str, refresh_token: str, expires_at: int = 0, expires_in: int = 0) None

Bases: Token

Wrapper for an OAuth token implementing expiration methods.

classmethod from_json(file_path: Path) OAuthToken
Return type:

OAuthToken

property is_expiring: bool
static is_oauth(headers: CaseInsensitiveDict) bool
Return type:

bool

update(fresh_access: BaseTokenDict)

Update access_token and expiration attributes with a BaseTokenDict inplace. expires_at attribute set using current epoch, avoid expiration desync by passing only recently requested tokens dicts or updating values to compensate.

class ytmusicapi.auth.oauth.RefreshingToken(scope: str | Literal['https://www.googleapis.com/auth/youtube'], token_type: str | Literal['Bearer'], access_token: str, refresh_token: str, expires_at: int = 0, expires_in: int = 0, credentials: Credentials | None = None, _local_cache: Path | None = None) None

Bases: OAuthToken

Compositional implementation of Token that automatically refreshes an underlying OAuthToken when required (credential expiration <= 1 min) upon access_token attribute access.

access_token: str
credentials: Optional[Credentials] = None

credentials used for access_token refreshing

property local_cache: Path | None
classmethod prompt_for_token(credentials: Credentials, open_browser: bool = False, to_file: str | None = None) RefreshingToken

Method for CLI token creation via user inputs.

Parameters:
  • credentials (Credentials) – Client credentials

  • open_browser (bool) – Optional. Open browser to OAuth consent url automatically. (Default: False).

  • to_file (Optional[str]) – Optional. Path to store/sync json version of resulting token. (Default: None).

Return type:

RefreshingToken

refresh_token: str
scope: Union[str, Literal['https://www.googleapis.com/auth/youtube']]
store_token(path: str | None = None) None

Write token values to json file at specified path, defaulting to self.local_cache. Operation does not update instance local_cache attribute. Automatically called when local_cache is set post init.

Return type:

None

token_type: Union[str, Literal['Bearer']]