【发布时间】:2016-11-03 03:56:00
【问题描述】:
我在 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/
【问题讨论】:
-
您找到解决方案了吗?
标签: python ajax django oauth spotipy