1

Hello I am trying to make a django website using the spotify api, so I am trying to get some simple example code working using the spotipy python library, but keep getting a http post 500 whenever my spotipy code is called.

Right now if you click a button on the website it makes a post request to one of my endpoints, which calls a python function and is supposed to return text. This is the code in the python function:

import spotipy

def spotifyleastplayed_py(request):
    print("spotifyleastplayed_py()")
    if request.method == 'POST':

        print("0")
        sp = spotipy.Spotify()
        print("1")
        results = sp.search(q='weezer', limit=20)
        print("2")
        print(results)

        data = "temp spotifyleastplayed_py() Return Data"
        return HttpResponse(data) #HttpResponse(json.dumps("{test:bobo}"))

When the function is called, my console outputs the following error message:

[06/Oct/2019 21:49:03] "GET /spotifyleastplayed HTTP/1.1" 200 1992
spotifyleastplayed_py()
0
1
[06/Oct/2019 21:49:07] "POST /spotifyleastplayed_py/ HTTP/1.1" 500 6326

Do I need to add the spotipy url to django somewhere so the library can make calls successfully? It seems like its failing to make the http request to spotipy.

Martin
  • 1,336
  • 4
  • 32
  • 69

1 Answers1

3

First of all, I would advise you to learn more about debugging your python code, as this is a critical skill to have as a developer, and it might help you get further into the problem next time. One thing you could deduce from your example for example is that your program does not execute anything beyond the following line

results = sp.search(q='weezer', limit=20)

But the only information you are getting is a 500 return code, which doesn't tell you exactly what is going wrong, only that something is not right.

One first step you could take for example is trying to find out what exactly is causing your code to terminate. If you wrap the statement in a try except block, you'll be able to see exactly what kind of error is occurring, like this:

try:
    results = sp.search(q='weezer', limit=20)
except Exception as e:
    print(e)

This catches the error generated by the statement, and prints it out which will give the following:

http status: 401, code:-1 - https://api.spotify.com/v1/search?q=weezer&limit=20&offset=0&type=track: No token provided

That's already a lot more telling than simply a 500 error, right? I would not recommend this method for every issue in your code, but it's a start.

To learn more about how to debug your code, you can read articles like this.


Anyways:

When I run your code, a spotipy.client.SpotifyException is raised, because the Spotify API returns a 401 error code.

401 (Unauthorized) means you have no authorization to access the requested resource, and for the Spotify API specifically, it means that you'll need to supply a valid token.

You'll need to request a token from the user, and pass that token when initializing spotify like this:

...
sp = spotipy.Spotify(auth=token)
results = sp.search(q='weezer', limit=20)
...

How exactly you get this token from the user depends on the rest of your implementation. I would recommend reading up on Spotify's authentication flow

There are also plenty of other examples on how people implemented the authorization flow in spotipy, for example in this StackOverflow thread.

Nico Griffioen
  • 5,143
  • 2
  • 27
  • 36