重新输入重定向 URL 时获取'spotipy.oauth2.SpotifyOauthError: Bad Request'



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

最初我将重定向 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

您尚未在函数中放置任何 id 或机密。当您为 Spotify 应用程序创建帐户时,您将获得一个 ID 和一个秘密,您还将确定重定向 URL。除了用户名之外,您还将使用这三个。我假设你可能已经解决了这个问题,但我正在为其他人给出这个答案。仔细阅读文档。

相关内容

  • 没有找到相关文章

最新更新