Spotipy on Django authorization without copy-paste to console

我与影子孤独终老i 提交于 2021-01-28 09:00:00

问题


I have a Django site in which I want to use spotipy to look for statistics of the song like popularity and views. I have this code right now:

import spotipy
import spotipy.util as util #luxury
import json
import webbrowser

username = 'dgrqnco2rx8hdu58kv9if9eho'
scope = 'user-read-private user-read-playback-state user-modify-playback-state'

token = util.prompt_for_user_token(username, scope, client_id='08bb526962574a46b359bffc56048147',
                                       client_secret='bf6d4184c8ae40aca207714e02153bad', redirect_uri='http://google.com/')

sp_obj = spotipy.Spotify(auth=token)
ss = 'name of song'
if ss.__contains__('('):
    q = ss[0:ss.index('(')]
elif ss.__contains__('['):
    q = ss[0:ss.index('[')]
elif ss.__contains__('['):
    q = ss[0:ss.index('{')]
else:
    q = ss
query = sp_obj.search(q, 1, 0, 'track')

#<<<<<<<<<<SONG>>>>>>>>>>

#FIND THE SONG URI
song_uri = query['tracks']['items'][0]['uri']

track = sp_obj.track(song_uri)
track_data = sp_obj.audio_features(song_uri)

song_popularity = track['popularity']
song_danceability = track_data[0]['danceability']
song_energy = track_data[0]['energy']
song_loudness = track_data[0]['loudness']
song_tempo = track_data[0]['tempo']

However spotipy redirects me to a page for authorization and I need to paste the url in the console. The regular user however does not have access to this console. So how can I do the authorization in an alternative way or even bypass it?

I was thinking about getting a spotify account in which every user will be getting logged in so that the user won't have to do the authorization and won't have to have a spotify account. Is this possible? If not what else can I try?


回答1:


You can't use util.prompt_for_user_token because it's just a helper for local usage only.

You need to arrange your code as API endpoints so that multiple users can sign in. Here is a full working example that would allow multiple users to sign in https://github.com/plamere/spotipy/blob/master/examples/app.py.

It uses Flask but you can easily adapt it to Django.



来源:https://stackoverflow.com/questions/61236458/spotipy-on-django-authorization-without-copy-paste-to-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!