Issue while fetching data from Spotify API. Getting Response 500
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Labels:
- Labels:
-
Possible Bug
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page