response 400 invalid request when trying to get user top items
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Plan
Free
Country
UK
My Question or Issue
This is my code and i assumer there is something wrong with my request however I am unsure of where it is coming from as I have requested a token, created the header and set the endpoint with the get request. I am open to any feedback
from dotenv import load_dotenv
import os
import base64
from requests import post,get
import requests
import json
import sys
load_dotenv()
client_id = os.getenv("CLIENT_ID")
client_secret = os.getenv("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")
#converts clinet id and secret and encodes them to a string so used in thr header
headers = {
#associated with our request and post request sent to url
"Authorization": "Basic " + auth_base64,
#authorization header to send in authorization data and verify evrything is correct to be sent back#
"Content-Type": "application/x-www-form-urlencoded"
}
data = {"grant_type": "client_credentials",
"scope":"user-top-read"}
result = post(url, headers=headers,data = data)
# to post(sends data as a rquest to the spotify web) the data we have gathered
json_result = json.loads(result.content)
#takes the returned json data(content)
token = json_result["access_token"]
return token
token = get_token()
def get_auth_header(token😞
return {"Authorization": "Bearer " + token}
#creates dictionary holding token
#used for authorization header for future requests
authentication_header = get_auth_header(token)
print(authentication_header)
def get_top_tracks(token😞
token = get_token()
response = get(url,headers=authentication_header)
return(response)
try:
data = response.json()
return(data)
except ValueError:
return("None")
#function to send request top get top tracks
tracks = get_top_tracks(token)
print(tracks)
if tracks.status_code == 400 :
error_message = tracks.text
print("Error:",error_message)
else:
print("nfn")
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