I
I
Insssane2021-12-03 16:04:44
Python
Insssane, 2021-12-03 16:04:44

Spotify API. When asked to get top Artists, Response 403. How to fix?

I want to get the top performers through the Spotify API, but I get an error 403 when I request it. I did everything as in the documentation https://developer.spotify.com/console/get-current-...

You can get an OAuth Token from the link above, and if you make a request everything works out through it, I don’t understand whether it is necessary to make a request for a new token or mine will do?
Access Token has been updated and if you make a request for several tracks, then everything works.

client_data = f'{client_id}:{client_secret}'
client_data_bytes = client_data.encode('ascii')
base64_bytes_client_data = base64.b64encode(client_data_bytes)
base64_client_data = base64_bytes_client_data.decode('ascii')


class RefreshToken:

    def __init__(self):
        self.refresh_token = refresh_token
        self.client_data = base64_client_data

    def refresh(self):

        query = 'https://accounts.spotify.com/api/token'

        response = requests.post(query, data={'grant_type': 'refresh_token', 'refresh_token': refresh_token},
                                 headers={'Authorization': 'Basic ' + self.client_data})

        response_object = response.json()
        return response_object['access_token']


obj = RefreshToken()
refreshed_token = obj.refresh()


def get_top_items(refreshed_token):

    type_item = 'artists'
    top_items_endpoint = f'https://api.spotify.com/v1/me/top/{type_item}?'

    get_params = {
        'time_range=': 'medium_term',
        'limit=': '10',
        'offset=': '5',
    }

    get_header = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + refreshed_token
    }

    res = requests.get(top_items_endpoint, params=get_params, headers=get_header)

    # top_objects = res.json()

    print(res)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-12-03
@Insssane

403, as a rule, indicates that the user does not have rights for this action, the token is invalid, the token has expired. How did you get the token, what scope did you transfer? Spotify clearly wants `user-top-read`, probably didn't pass it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question