首页 文章

Spotipy - 无法登录进行身份验证(授权代码流程)

提问于
浏览
1

我正在使用Spotipy Python库连接到Spotify Web API . 我想通过Authorization Code Flow访问我的Spotify用户帐户 . 我使用的是Python 3.5,Spotipy 2.4.4,谷歌Chrome 55.0.2883.95(64位)和Mac OS Sierra 10.12.2

首先,我去the Spotify Developer website注册程序以获取客户端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的凭证以授予对我的应用的访问权限 . 但是,当我最终点击“登录”(或西班牙语中的“Iniciarsesión”)时,没有任何反应 . 我尝试使用我的Facebook帐户登录,但它也不起作用 . 每次点击“登录”时,我都会收到错误请求 . 以下是Chrome的捕获:

enter image description here

该过程不完整,因为当尝试在终端上输入重定向URI时,我得到另一个错误请求:

raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request

我试图重新启动计算机,清理我的浏览器cookie,使用另一个不同的浏览器,但没有工作 . 似乎I am not the only onethis problem . 这可能是一个错误吗?请避免像“阅读API的文档here”这样的答案 . 谢谢 .

1 回答

  • 0

    解决了它 . 为了正确运行Spotipy文档中提供的Authorization Code Flow example code,我在调用util.prompt_for_user_token时在示例脚本的第13行中指定了重定向URI,即使我之前在设置环境变量时已经这样做了:

    token = util.prompt_for_user_token(username, scope, redirect_uri = 'https://example.com/callback/')
    

    同样,请勿使用https://www.google.com或类似的网址作为重定向URI . 相反,请按照建议here尝试'https://example.com/callback/ ' or ' http://localhost/' . 不要忘记,登录后重定向的URL必须包含单词代码 .

    干杯,

相关问题