Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Issue while fetching data from Spotify API. Getting Response 500

Issue while fetching data from Spotify API. Getting Response 500

Hello,

I am new to Spotify API and I am trying to get the data of recently played tracks using python. I created 2 files. One is main.py and I wanted to test if I am recieving any response from the API and I created test.py for testing. The code present in main.py is as follows:

 

import json
from dotenv import load_dotenv
import os
import base64
from requests import post

load_dotenv()

client_id = os.getenv("CLIENT_ID")
client_secret = os.getenv("CLIENT_SECRET")
 
#print(client_id, client_secret)

def get_token():
    auth_string = client_id + ":" + client_secret
    auth_bytes = auth_string.encode("utf-8")
    auth_base64 = str(base64.b64encode(auth_bytes), "utf-8")

    headers = {
        "Authorization": "Basic " + auth_base64,
        "Content-Type": "application/x-www-form-urlencoded"
    }
    data = {
        "grant_type": "client_credentials",
        "scope": "user-read-recently-played"
    }
    result = post(url, headers=headers, data=data)
    json_result = json.loads(result.content)
    token = json_result["access_token"]
    return token

token = get_token()
#print(token)
 
and the code in test.py is as follows:
 
import pandas as pd
import requests
from datetime import datetime
import datetime
from main import get_token


#USER_ID = "YOUR_USER_ID"
TOKEN = get_token()



input_variables = {
    "Accept" : "application/json",
    "Content-Type" : "application/json",
    "Authorization" : "Bearer {token}".format(token=TOKEN)
}

today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1) #no of Days u want the data for)
yesterday_unix_timestamp = int(yesterday.timestamp()) * 1000

# Download all songs you've listened to "after yesterday", which means in the last 24 hours      
r = requests.get("https://api.spotify.com/v1/me/player/recently-played?limit=50&after={time}".format(time=yesterday_unix_timestamp), headers=input_variables)
data = r.json()

print(r)
 
whenever I run test.py, I keep getting <Response[500]> and not able to fetch the data. I would be glad if someone can help me with this issue.
Reply
3 Replies

An indication  of your problem is 'grant_type': 'client_credentials'

Since the Client Credentials flow does not include authorization, only endpoints that do not access user information can be accessed. Therefore, in this case, you should use a different Authorization method that lets you log in.

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

Can you please suggest the authorization method I can use I am very new to using APIs and cannot understand what to use.

I prefer the Authorization Code flow. Here is a stack overflow answer with  code by someone else: https://stackoverflow.com/a/75292843/7111585

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

Suggested posts