예제 #1
0
def _client_credentials(conf):
    auth_key = get_auth_key(conf.client_id, conf.client_secret)

    headers = {
        'Authorization': f'Basic {auth_key}',
    }

    options = {
        'grant_type': 'client_credentials',
        'json': True,
    }

    response = requests.post('https://accounts.spotify.com/api/token',
                             headers=headers,
                             data=options)

    content = json.loads(response.content.decode('utf-8'))

    if response.status_code == 400:
        error_description = content.get('error_description', None)
        raise BadRequestError(error_description)

    access_token = content.get('access_token', None)
    token_type = content.get('token_type', None)
    expires_in = content.get('expires_in', None)
    scope = content.get('scope', None)

    return Authorization(access_token, token_type, expires_in, scope, None)
예제 #2
0
def _refresh_access_token(auth_key, refresh_token):
    headers = {
        'Authorization': f'Basic {auth_key}',
    }

    options = {
        'refresh_token': refresh_token,
        'grant_type': 'refresh_token',
    }

    response = requests.post('https://accounts.spotify.com/api/token',
                             headers=headers,
                             data=options)

    content = json.loads(response.content.decode('utf-8'))

    if not response.ok:
        error_description = content.get('error_description', None)
        raise BadRequestError(error_description)

    access_token = content.get('access_token', None)
    token_type = content.get('token_type', None)
    scope = content.get('scope', None)
    expires_in = content.get('expires_in', None)

    return Authorization(access_token, token_type, expires_in, scope, None)
예제 #3
0
def _authorization_code_request(auth_code):
    config = read_config()
    auth_key = get_auth_key(config.client_id, config.client_secret)
    headers = {'Authorization': f'Basic {auth_key}'}

    options = {
        'code': auth_code,
        'redirect_uri': 'http://localhost:3000/callback',
        'grant_type': 'authorization_code',
        'json': True
    }
    response = requests.post(config.access_token_url,
                             headers=headers,
                             data=options)

    content = json.loads(response.content.decode('utf-8'))

    if response.status_code == 400:
        error_description = content.get('error_description', '')
        raise BadRequestError(error_description)

    access_token = content.get('access_token', None)
    token_type = content.get('token_type', None)
    expires_in = content.get('expires_in', None)
    scope = content.get('scope', None)
    refresh_token = content.get('refresh_token', None)
    return Authorization(access_token, token_type, expires_in, scope,
                         refresh_token)
예제 #4
0
def _refresh_access_token(auth_key, refresh_token):

    headers = {
        "Authorization": "Basic {}".format(auth_key),
    }

    options = {"refresh_token": refresh_token, "grant_type": "refresh_token"}

    response = requests.post('https://accounts.spotify.com/api/token',
                             headers=headers,
                             data=options)

    content = json.loads(response.content.decode('utf-8'))

    if not response.ok:
        error_description = content.get('error_description', "")
        raise BadRequestError(error_description)

    access_token = content.get('access_token', None)
    token_type = content.get('token_type', None)
    expires_in = content.get("expires_in", None)
    scope = content.get("scope", None)

    return Authorization(access_token, token_type, expires_in, scope, None)
예제 #5
0
def _client_credentials(conf):  # Takes an argument of the configuration
    """
    uses the get_auth_key function to pass the client_id
    and the client_secret to build a base 64-encoded auth_key
    """
    auth_key = get_auth_key(conf.client_id, conf.client_secret)

    # setting the Authorization in the request header
    headers = {
        'Authorization': f'Basic {auth_key}',
    }

    options = {
        'grant_type': 'client_credentials',
        'json': True,  # tells the API that we want the response in JSON format
    }
    """
    Use the requests package to make the request to spotify's account service
    passing the headers and data configured
    """
    response = requests.post('https://accounts.spotify.com/api/token',
                             headers=headers,
                             data=options)

    content = json.loads(response.content.decode('utf-8'))
    # After getting a response the JSON data gets decoded and loaded into the variable content
    if response.status_code == 400:
        error_description = content.get('error_description', '')
        raise BadRequestError(error_description)

    access_token = content.get('access_token', None)
    token_type = content.get('token_type', None)
    expires_in = content.get('expires_in', None)
    scope = content.get('scope', None)

    return Authorization(access_token, token_type, expires_in, scope, None)