python - 如何使用客户端 ID 和密码访问 spotify 的 Web api?
问题描述
我正在尝试编写一个脚本,从头开始在 python 中的我的 spotify 帐户上创建一个播放列表,而不是使用像spotipy这样的模块。
我的问题是如何使用请求模块对我的客户端 ID 和客户端密钥进行身份验证,或者如何使用这些凭据获取访问令牌?
解决方案
试试这个完整的客户端凭据授权流程。
第一步 - 使用您的凭据获取授权令牌:
CLIENT_ID = " < your client id here... > "
CLIENT_SECRET = " < your client secret here... > "
grant_type = 'client_credentials'
body_params = {'grant_type' : grant_type}
url='https://accounts.spotify.com/api/token'
response = requests.post(url, data=body_params, auth = (CLIENT_ID, CLIENT_SECRET))
token_raw = json.loads(response.text)
token = token_raw["access_token"]
第二步——向任何播放列表端点发出请求。确保为 设置有效值<spotify_user>
。
headers = {"Authorization": "Bearer {}".format(token)}
r = requests.get(url="https://api.spotify.com/v1/users/<spotify_user>/playlists", headers=headers)
print(r.text)
推荐阅读
- sql - 格式化字段中的 LEN() 和 CHARINDEX() 问题
- java - 不支持的消息类型 - SecurityList + FIX50SP2
- botframework - Directline和Webchat?异同
- swift - 如何从 Alarmofire 获取 JSON 值并以表格格式弹出?
- python - Python Pandas 中的 Excel SUMIFS 数组
- rust - 如果条件为假,我可以在 Rust 中使用什么运算符来返回特定错误?
- php - 我想用 prestashop 订单添加额外的数据
- java - 如何为 Filebeat 配置 JBoss JsonFormatter (WildFly 14)
- javascript - 如何在nodejs上为postgresql数据库编写单元测试
- cucumber - 班级
无法解析为 POM.xml 中的类型