-2

I am trying to use spotipy to execute a search request from Spotify. I specified the client id, secret id and redirect url (http://127.0.0.1:8000/callback/q) in my bash_profiles, as described in the API.

def search(username, query):

    token = util.prompt_for_user_token(username, scope) #like oauth with redirect url
    print("received token response")

    if token:
        sp = spotipy.Spotify(auth=token)
        return sp.search(query, 1, 0, type='track')

The problem is I am using django, which also forces me to specify the redirect url in my urls.py and a corresponding view in views.py

    [from urls.py...]
    url(r'^callback/q$', views.callback, name='callback'),

    [from views.py...]
    def callback(request):
        print("callback view reached")
        return render(request, 'rec/search.html')

It seems like django then intercepts displays the view from the redirect-url, so my search method never finishes executing. I am a little confused on how I get a token that requires a redirect url, yet then continue executing the rest of the method that follows the authorization request

tara
  • 19
  • 1
  • 7
  • This doesn't make a lot of sense. What does your bash_profile have to do with anything? Why are you *prompting* for a token in a web app? – Daniel Roseman Aug 08 '16 at 15:24
  • that's how the Spotipy api works. You use a util method to get a token, create a spotify object and call methods on it. For the bash profile-- you can choose to export the variables (like i did) or pass them as args to the auth method – tara Aug 08 '16 at 15:33
  • to be more specific on the "user prompting". it does open up a page on spotify and the user has to agree to give certain privileges. i believe that is first time auth only, not on refresh tokens – tara Aug 08 '16 at 15:35
  • @tara did you ever figure it out? Facing similar issues myself – stackPusher Feb 03 '17 at 22:21

1 Answers1

0

I'm not sure if this helps but I abandoned authentication via util.prompt_for_user_token in favor of this basic implementation. It's implemented in bottle but should be relatively easy to translate to django and your specific needs. I'm hoping to do the same.

Community
  • 1
  • 1
  • thank you! will see what i can do with this and add here if i find anything additional to be of use. though am still very curious what i was doing wrong, as it seems like auth-with-redirect is a common pattern. i had a little better luck splitting the auth token out into a separate method but still was not up to working 100% – tara Aug 10 '16 at 00:07