【问题标题】:Getting 'spotipy.oauth2.SpotifyOauthError: Bad Request' when re-inputting the redirect URL重新输入重定向 URL 时获取 'spotipy.oauth2.SpotifyOauthError: Bad Request'
【发布时间】:2019-08-14 03:21:56
【问题描述】:

我正在尝试使用 util.prompt_user_for_token() 方法完成 spotipy 身份验证流程 - 我的总体目标只是能够创建用户播放列表。当我运行我的代码时,我被定向到我的重定向 URL 页面,该 URL 现在按预期附加了一个“code=”段。当我尝试在出现提示时将此 URL 复制回该字段时,我总是收到“spotipy.oauth2.SpotifyOauthError: Bad Request”。

最初我将重定向 URL 设置为 https://www.google.ca/,但在阅读了其他一些线程后,我又回到了文档中也提到的“https://example.com/callback/”,但这并没有解决问题。我将不胜感激任何指针!

def authenticate(username):
    scope = 'playlist-modify-public'
    token = util.prompt_for_user_token(
        username=username,
        scope=scope,
        client_id='',
        client_secret='',
        redirect_uri='https://example.com/callback/'
    )

    return token


def createPlaylist(authToken, username):
    if authToken:
        spotifyObject = spotipy.Spotify(authToken)
        spotifyObject.trace = False
        spotifyObject.user_playlist_create(spotifyUsername, 'playlistTestName')
    else:
        print("Can't get token for", username)
    return


authToken = authenticate(spotifyUsername)
createPlaylist(authToken, spotifyUsername)

这是我收到的完整错误消息:

Traceback (most recent call last):
  File "PlaylistGenerator.py", line 31, in <module>
    authToken = authenticate(spotifyUsername)
  File "PlaylistGenerator.py", line 15, in authenticate
    redirect_uri='https://example.com/callback/'
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/util.py", line 86, in prompt_for_user_token
    token_info = sp_oauth.get_access_token(code)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spotipy/oauth2.py", line 217, in get_access_token
    raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request

【问题讨论】:

    标签: python authentication oauth-2.0 spotify spotipy


    【解决方案1】:

    您没有在函数中添加任何 id 或 secret。当您为 spotify 应用程序创建帐户时,您将获得一个 id 和一个秘密,您还将确定一个重定向 url。除了用户名之外,您还将使用这三个。我假设您可能已经解决了这个问题,但我正在为其他人提供这个答案。仔细阅读文档。

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 1970-01-01
      • 2013-09-07
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 2015-07-22
      • 1970-01-01
      相关资源
      最近更新 更多