使用Spotipy调用user_create_playlist报错:URL不允许该方法
解决Spotipy调用user_create_playlist时的"URL不允许该方法"错误
嘿,我一眼就瞅出问题出在哪了——你的redirect_uri配置完全错啦!这个报错的核心原因很明确:
- 你代码里用的
https://developer.spotify.com/是Spotify的官方文档地址,它根本不允许作为OAuth回调地址来接收授权码,Spotify服务器自然会拒绝你的请求 - 另外Spotify有个硬要求:
redirect_uri必须和你在Spotify开发者控制台里注册应用时填写的地址完全一致,连大小写、末尾斜杠这种细节都不能差
一步步修复:
1. 先更新开发者控制台的回调地址
登录你的Spotify开发者账号,找到你创建的应用,进入"编辑设置"页面:
- 添加一个有效的回调地址,本地测试最常用的是
http://localhost:8080(只要是你能控制、可以接收HTTP请求的地址都行) - 别忘了点击保存更改
2. 修改代码里的redirect_uri
把代码中的redirect_uri替换成你刚才在控制台里设置的地址,确保完全匹配。修正后的代码示例:
import spotipy import spotipy.util as util username = 'my-username' token = util.prompt_for_user_token( username=username, scope='playlist-modify-public', client_id='my-spotify-client-id', client_secret='my-spotify-client-secret-id', redirect_uri='http://localhost:8080' # 这里改成你控制台里的地址 ) if token: spotifyObject = spotipy.Spotify(auth=token) # 调用创建播放列表的方法 new_playlist = spotifyObject.user_playlist_create( user=username, name="My Custom Playlist", public=True, description="Built with Spotipy" ) print(f"Playlist created! ID: {new_playlist['id']}") else: print(f"Failed to get token for user {username}")
3. 清理旧缓存(可选但推荐)
之前用错误redirect_uri生成的token缓存可能还留在本地,你可以删除.cache-<你的用户名>这个文件,再重新运行代码,确保拿到全新的有效token。
额外小提示:
- 如果要创建私有播放列表,记得把
scope改成playlist-modify-private,同时调用user_playlist_create时把public参数设为False - 个人测试用的应用不需要通过Spotify的开发者审核,直接用就行
内容的提问来源于stack exchange,提问作者jfaul
相关产品推荐
相关产品推荐

