Announcements

Help Wizard

Step 1

NEXT STEP

GET request for top tracks returns an empty string

GET request for top tracks returns an empty string

So I've been trying to fetch my top songs with the code below, but the get request is returning an empty string. What exactly am I doing wrong?

-----------------------------------------

import json
import requests

CLIENT_ID = 'xxxxx'
CLIENT_SECRET = 'xxxxx'


# POST request
auth_response = requests.post(AUTH_URL, {
    'grant_type': 'client_credentials',
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET,
})

# converting the response to JSON
auth_response_data = auth_response.json()

# saving the access token
access_token = auth_response_data['access_token']

headers = {
    'Authorization': 'Bearer {token}'.format(token=access_token)
}

# base URL

#GET request

r = requests.get(BASE_URL + 'me/top/tracks', headers = headers, params = {'limit': 50, 'offset': 0, 'time_range': 'medium_term'})

data = r.text
 
#this print statement below prints out an empty string
print(data)
 
#the rest of the code is commented out because json.loads() gives an
#error when trying to convert an empty string
 
# parsed_json = json.loads(data)

# songs = parsed_json['items']

# print(songs)

 

Reply
2 Replies

I have the exact same issue

The issue in this case is that you can't use Client Credentials with this endpoint. "Since this flow does not include authorization, only endpoints that do not access user information can be accessed."

You should use another Authorization method instead. (with PKCE if the authorization is happening fully in the browser.)

Suggested posts