我正在使用 Spotipy Python 库连接到 Spotify Web API。我想通过Authorization Code Flow访问我的 Spotify 用户帐户。我正在使用 Python 3.5、Spotipy 2.4.4、Google Chrome 55.0.2883.95(64 位)和 Mac OS Sierra 10.12.2
首先,我去Spotify 开发者网站注册程序以获取客户端 ID、客户端密钥并在我的白名单上输入重定向 URI ( https://www.google.com )。
其次,我从终端设置环境变量:
export SPOTIPY_CLIENT_ID='my_spotify_client_id'
export SPOTIPY_CLIENT_SECRET='my_spotify_client_secret'
export SPOTIPY_REDIRECT_URI='https://www.google.com'
然后我尝试从终端运行示例代码,键入“python3.5 script.py my_username”。这是脚本:
import sys
import spotipy
import spotipy.util as util
scope = 'user-library-read'
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print "Usage: %s username" % (sys.argv[0],)
sys.exit()
token = util.prompt_for_user_token(username, scope)
if token:
sp = spotipy.Spotify(auth=token)
results = sp.current_user_saved_tracks()
for item in results['items']:
track = item['track']
print track['name'] + ' - ' + track['artists'][0]['name']
else:
print "Can't get token for", username
运行此代码时,它会将我带到浏览器上的登录屏幕。我输入我的 Spotify 凭据以授予对我的应用程序的访问权限。但是当我最终点击“登录”(或西班牙语中的“Iniciar sesión”)时,什么也没有发生。我尝试使用我的 Facebook 帐户登录,但它也不起作用。似乎每次单击“登录”时都会收到错误请求。这是 Chrome 的截图:
该过程不完整,因为当尝试在终端上输入重定向 URI 时,我收到另一个错误请求:
raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request
我尝试重新启动计算机,清理浏览器 cookie,使用另一个不同的浏览器,但没有成功。看来我不是唯一一个有这个问题的人。这可能是一个错误吗?请避免诸如“阅读此处的 API 文档”之类的答案。谢谢你。
