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

response 400 invalid request when trying to get user top items

response 400 invalid request when trying to get user top items

 

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")  
Reply
1 Reply

Please read the top of the following page again: https://developer.spotify.com/documentation/web-api/tutorials/client-credentials-flow

And choose another flow from here: https://developer.spotify.com/documentation/web-api/concepts/authorization

XimzendRising 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