我在 Django 中构建了一个应用程序,它使用 Spotify API Python 库 Spotipy,并使用该spotipy.util.prompt_for_user_token()
命令生成令牌并访问我的私有库:
import spotipy
import spotipy.util as util
import os, ast
#Spotify API keys
scope = "playlist-read-private"
uir = "http://localhost:8000"
username = "<MY_USERNAME>"
spotify_uid = os.environ["SPOTIFY_UID"]
spotify_usec = os.environ["SPOTIFY_USEC"]
print "retrieved keys from OS"
#set up access
def get_access():
try:
token = util.prompt_for_user_token(username, scope, spotify_uid, spotify_usec, uir)
print "SUCCESS"
return spotipy.Spotify(auth=token)
except:
print "FAILED TO LOAD"
我希望该应用程序具有 ui 登录而不是硬编码登录,但我不知道如何。
目前,我有一个“登录”按钮,它尝试通过 Javascript 触发登录页面重定向,方法是使用用户名参数调用上面的代码,但这会打开一个新页面,控制台中有以下内容:
User authentication requires interaction with your
web browser. Once you enter your credentials and
give authorization, you will be redirected to
a url. Paste that url you were directed to to
complete the authorization.
Opening https://accounts.spotify.com/authorize?scope=playlist-read- private&redirect_uri=http%3A%2F%2Flocalhost%3A8000&response_type=code&client_id=<CLIENT_ID> in your browser
Enter the URL you were redirected to: [30/Jun/2016 15:53:54] "GET /?code=<TOKEN>HTTP/1.1" 200 2881
注意: 克拉括号中的文本已被替换,因为它们是私钥。
我希望它具有与本网站处理登录方式类似的功能:http: //static.echonest.com/SortYourMusic/