Skip to content

Authentification

The AuthorisationToken is used to authorize the requests which get made. Every token has an activation_time and a access_token property which have to be present. The refresh_token property is only present if you use a authorization flow which provides a refresh_token.

SpotifyAuthorisationToken

Class that has the Authorisation Token

Source code in async_spotify/authentification/spotify_authorization_token.py
class SpotifyAuthorisationToken:
    """
    Class that has the Authorisation Token
    """

    def __init__(self, refresh_token: str = None, activation_time: int = None, access_token: str = None):
        """
        Generate a new authorisation token

        Args:
            refresh_token: The refresh token that was given to the application
                (not needed if client credentials workflow is used)
            access_token: The token that will be used to make request
        """

        self.activation_time: int = activation_time
        self.refresh_token: str = refresh_token
        self.access_token: str = access_token

    def is_expired(self) -> bool:
        """
        Checks if the api token has expired

        Returns:
            Is the token expired
        """

        current_time: int = int(time.time())

        # Check if token is valid (3600 would be correct, but a bit of time padding is always nice)
        if current_time - self.activation_time > 3400:
            return True

        return False

    @property
    def valid(self) -> bool:
        """
        Validate that the token is not partially empty

        Returns:
            Is the token valid
        """

        if self.access_token and self.activation_time:
            return True
        return False

    def __eq__(self, other) -> bool:
        """
        Support for equal assertion

        Args:
            other: The other object the comparison is made to

        Returns:
            Is the content of the objects equal
        """
        return self.__dict__ == other.__dict__

valid: bool property readonly

Validate that the token is not partially empty

Returns:

Type Description
bool

Is the token valid

__eq__(self, other) special

Support for equal assertion

Parameters:

Name Type Description Default
other

The other object the comparison is made to

required

Returns:

Type Description
bool

Is the content of the objects equal

Source code in async_spotify/authentification/spotify_authorization_token.py
def __eq__(self, other) -> bool:
    """
    Support for equal assertion

    Args:
        other: The other object the comparison is made to

    Returns:
        Is the content of the objects equal
    """
    return self.__dict__ == other.__dict__

__init__(self, refresh_token=None, activation_time=None, access_token=None) special

Generate a new authorisation token

Parameters:

Name Type Description Default
refresh_token str

The refresh token that was given to the application (not needed if client credentials workflow is used)

None
access_token str

The token that will be used to make request

None
Source code in async_spotify/authentification/spotify_authorization_token.py
def __init__(self, refresh_token: str = None, activation_time: int = None, access_token: str = None):
    """
    Generate a new authorisation token

    Args:
        refresh_token: The refresh token that was given to the application
            (not needed if client credentials workflow is used)
        access_token: The token that will be used to make request
    """

    self.activation_time: int = activation_time
    self.refresh_token: str = refresh_token
    self.access_token: str = access_token

is_expired(self)

Checks if the api token has expired

Returns:

Type Description
bool

Is the token expired

Source code in async_spotify/authentification/spotify_authorization_token.py
def is_expired(self) -> bool:
    """
    Checks if the api token has expired

    Returns:
        Is the token expired
    """

    current_time: int = int(time.time())

    # Check if token is valid (3600 would be correct, but a bit of time padding is always nice)
    if current_time - self.activation_time > 3400:
        return True

    return False

SpotifyCookie

This class represents a spotify cookie. All of the provided values have to be set, otherwise the request will not be successful.

Source code in async_spotify/authentification/spotify_cookies.py
class SpotifyCookie:
    """
    This class represents a spotify cookie. All of the provided values have to be set, otherwise the request will not be
    successful.
    """

    def __init__(self, sp_t: str = None, sp_dc: str = None, sp_key: str = None):
        """
        Create a new spotify cookie. All values have to be set if you want to use it

        Args:
            sp_t: The name of the spotify cookie (Use the raw value of the cookie as value of this)
            sp_dc: The name of the spotify cookie (Use the raw value of the cookie as value of this)
            sp_key: The name of the spotify cookie (Use the raw value of the cookie as value of this)
        """
        self.sp_t: str = sp_t
        self.sp_dc: str = sp_dc
        self.sp_key: str = sp_key

    @property
    def valid(self) -> bool:
        """
        Check if all values are set correctly
        Returns:

        """
        if self.sp_t and self.sp_dc and self.sp_key:
            return True
        return False

    def load_from_file(self, file_path: str) -> None:
        """
        Load the cookies from a file

        Args:
            file_path: The cookie file path
        """
        with open(file_path) as file:
            file_json: dict = json.load(file)

        self.sp_t = file_json['sp_t']
        self.sp_dc = file_json['sp_dc']
        self.sp_key = file_json['sp_key']

valid: bool property readonly

Check if all values are set correctly

__init__(self, sp_t=None, sp_dc=None, sp_key=None) special

Create a new spotify cookie. All values have to be set if you want to use it

Parameters:

Name Type Description Default
sp_t str

The name of the spotify cookie (Use the raw value of the cookie as value of this)

None
sp_dc str

The name of the spotify cookie (Use the raw value of the cookie as value of this)

None
sp_key str

The name of the spotify cookie (Use the raw value of the cookie as value of this)

None
Source code in async_spotify/authentification/spotify_cookies.py
def __init__(self, sp_t: str = None, sp_dc: str = None, sp_key: str = None):
    """
    Create a new spotify cookie. All values have to be set if you want to use it

    Args:
        sp_t: The name of the spotify cookie (Use the raw value of the cookie as value of this)
        sp_dc: The name of the spotify cookie (Use the raw value of the cookie as value of this)
        sp_key: The name of the spotify cookie (Use the raw value of the cookie as value of this)
    """
    self.sp_t: str = sp_t
    self.sp_dc: str = sp_dc
    self.sp_key: str = sp_key

load_from_file(self, file_path)

Load the cookies from a file

Parameters:

Name Type Description Default
file_path str

The cookie file path

required
Source code in async_spotify/authentification/spotify_cookies.py
def load_from_file(self, file_path: str) -> None:
    """
    Load the cookies from a file

    Args:
        file_path: The cookie file path
    """
    with open(file_path) as file:
        file_json: dict = json.load(file)

    self.sp_t = file_json['sp_t']
    self.sp_dc = file_json['sp_dc']
    self.sp_key = file_json['sp_key']

AuthorizationCodeFlow (AuthorizationFlow)

A Class which implements the authorization flow

Notes

Scopes available:

ugc-image-upload
user-read-playback-state
user-read-email
playlist-read-collaborative
user-modify-playback-state
user-read-private
playlist-modify-public
user-library-modify
user-top-read
user-read-currently-playing
playlist-read-private
user-follow-read app-remote-control
user-read-recently-played
playlist-modify-private
user-follow-modify
user-library-read

Source code in async_spotify/authentification/authorization_flows/authorization_code_flow.py
class AuthorizationCodeFlow(AuthorizationFlow):
    """
    A Class which implements the authorization flow

    Notes:
        __Scopes available:__
        ```
        ugc-image-upload
        user-read-playback-state
        user-read-email
        playlist-read-collaborative
        user-modify-playback-state
        user-read-private
        playlist-modify-public
        user-library-modify
        user-top-read
        user-read-currently-playing
        playlist-read-private
        user-follow-read app-remote-control
        user-read-recently-played
        playlist-modify-private
        user-follow-modify
        user-library-read
        ```
    """

    def __init__(self, application_id: str = None, application_secret: str = None, scopes: List[str] = None,
                 redirect_url: str = None):
        """
        Create a new Spotify AuthorizationCodeFlow Object

        Args:
            application_id: The id of the application
            application_secret: The secret of the application
            scopes: The spotify scopes you app will request
            redirect_url: The redirect url spotify will redirect the user after authentication
        """

        self.application_id: str = application_id
        self.application_secret: str = application_secret
        self.scopes: List[str] = scopes
        self.redirect_url: str = redirect_url

    @property
    def valid(self) -> bool:
        """
        Validate if the auth_code_flow can be used. This will only check if the values of the auth_code_flow are not
        empty.

        Returns:
            Are the auth_code_flow valid
        """
        if self.application_id and self.application_secret and self.redirect_url:
            return True
        return False

valid: bool property readonly

Validate if the auth_code_flow can be used. This will only check if the values of the auth_code_flow are not empty.

Returns:

Type Description
bool

Are the auth_code_flow valid

__init__(self, application_id=None, application_secret=None, scopes=None, redirect_url=None) special

Create a new Spotify AuthorizationCodeFlow Object

Parameters:

Name Type Description Default
application_id str

The id of the application

None
application_secret str

The secret of the application

None
scopes List[str]

The spotify scopes you app will request

None
redirect_url str

The redirect url spotify will redirect the user after authentication

None
Source code in async_spotify/authentification/authorization_flows/authorization_code_flow.py
def __init__(self, application_id: str = None, application_secret: str = None, scopes: List[str] = None,
             redirect_url: str = None):
    """
    Create a new Spotify AuthorizationCodeFlow Object

    Args:
        application_id: The id of the application
        application_secret: The secret of the application
        scopes: The spotify scopes you app will request
        redirect_url: The redirect url spotify will redirect the user after authentication
    """

    self.application_id: str = application_id
    self.application_secret: str = application_secret
    self.scopes: List[str] = scopes
    self.redirect_url: str = redirect_url

ClientCredentialsFlow (AuthorizationFlow)

Class which should be used for the Client credentials flow

Source code in async_spotify/authentification/authorization_flows/client_credentials_flow.py
class ClientCredentialsFlow(AuthorizationFlow):
    """
    Class which should be used for the Client credentials flow
    """

    def __init__(self, application_id: str = None, application_secret: str = None, ):
        """
        Create a new Spotify AuthorizationCodeFlow Object

        Args:
            application_id: The id of the application
            application_secret: The secret of the application
        """

        self.application_id: str = application_id
        self.application_secret: str = application_secret

    @property
    def valid(self) -> bool:
        """
        Check if the ClientCredentialsFlow is valid
        Returns: boolean
        """
        if self.application_id and self.application_secret:
            return True
        return False

valid: bool property readonly

Check if the ClientCredentialsFlow is valid Returns: boolean

__init__(self, application_id=None, application_secret=None) special

Create a new Spotify AuthorizationCodeFlow Object

Parameters:

Name Type Description Default
application_id str

The id of the application

None
application_secret str

The secret of the application

None
Source code in async_spotify/authentification/authorization_flows/client_credentials_flow.py
def __init__(self, application_id: str = None, application_secret: str = None, ):
    """
    Create a new Spotify AuthorizationCodeFlow Object

    Args:
        application_id: The id of the application
        application_secret: The secret of the application
    """

    self.application_id: str = application_id
    self.application_secret: str = application_secret

ClientCredentialsFlow (AuthorizationFlow)

Class which should be used for the Client credentials flow

Source code in async_spotify/authentification/authorization_flows/client_credentials_flow.py
class ClientCredentialsFlow(AuthorizationFlow):
    """
    Class which should be used for the Client credentials flow
    """

    def __init__(self, application_id: str = None, application_secret: str = None, ):
        """
        Create a new Spotify AuthorizationCodeFlow Object

        Args:
            application_id: The id of the application
            application_secret: The secret of the application
        """

        self.application_id: str = application_id
        self.application_secret: str = application_secret

    @property
    def valid(self) -> bool:
        """
        Check if the ClientCredentialsFlow is valid
        Returns: boolean
        """
        if self.application_id and self.application_secret:
            return True
        return False

valid: bool property readonly

Check if the ClientCredentialsFlow is valid Returns: boolean

__init__(self, application_id=None, application_secret=None) special

Create a new Spotify AuthorizationCodeFlow Object

Parameters:

Name Type Description Default
application_id str

The id of the application

None
application_secret str

The secret of the application

None
Source code in async_spotify/authentification/authorization_flows/client_credentials_flow.py
def __init__(self, application_id: str = None, application_secret: str = None, ):
    """
    Create a new Spotify AuthorizationCodeFlow Object

    Args:
        application_id: The id of the application
        application_secret: The secret of the application
    """

    self.application_id: str = application_id
    self.application_secret: str = application_secret

Last update: August 28, 2020