Python-Spotify API返回错误400“格式错误的JSON"; [英] Python - Spotify API returning Error 400 "Malformed JSON"

查看:224
本文介绍了Python-Spotify API返回错误400“格式错误的JSON";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Heyo.我正尝试在业余时间制作一个使用Spotify API的小型应用程序.我设法使我的程序使用oAuth 2来让用户授权我的应用来操作他们的Spotify,但是我遇到了Spotify API上某个终结点的问题.

Heyo. I'm trying to make a small application in my spare time that uses the Spotify API . I have managed to get my program to use oAuth 2 to let a user authorize my app to manipulate their Spotify, but I have run into a problem with a certain endpoint on the Spotify API.

我遇到麻烦的端点是 https://api.spotify.com /v1/me/player/play (这是指向端点 https://developer.spotify.com/console/put-play/).每当我尝试向端点发出放置请求时,即使我从文档中复制/粘贴了自己的json,我也会收到一条带有消息格式错误的json"的400状态代码,但仍会收到此消息,因此我认为这不是问题关于我格式化json的方式,此外,我之前曾使用json来调用其他终结点,并且这些终结点在我的格式化上没有问题.

The endpoint I am having trouble with is https://api.spotify.com/v1/me/player/play (here's a link to their docs for the endpoint https://developer.spotify.com/console/put-play/). Whenever I try to make a put request to the endpoint I receive a 400 status code with the message "Malformed json" I get this message even when I copy/paste their own json from the docs, so I don't think it's a problem with how I am formatting my json, besides I have used json before to call other endpoints and they haven't had a problem with my formatting on those calls.

这是我的代码:

headers = {"Authorization":"Bearer {}".format(access_token)}

url = 'https://api.spotify.com/v1/me/player/play'
payload = {"context_uri": "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr"}
r = requests.put(url, headers=headers, data=payload)
print(r)
print(r.text)

为了明确起见,access_token是我从其授权过程中获得的访问令牌,并且我正在使用python-requests发出http请求(以下是该文档: https://requests.kennethreitz.org/en/master/)

To clarify, access_token is the access token that I have gotten from their authorization process, and I am using python-requests to make the http requests (Here is the docs for that: https://requests.kennethreitz.org/en/master/)

我想知道问题是否是由于Spotify在其轨道ID中使用冒号,而在JSON中也使用了冒号?我在这里的另一个线程中看到,我应该尝试在标题中添加"Content-Type":"application/json",但这丝毫没有改变结果.

I am wondering if the problem is due to the fact that Spotify uses colons int their track IDs and colons are also used in JSON? I saw in another thread on here that I should try to add "Content-Type":"application/json" to my headers but that didn't change the outcome at all.

我们非常感谢您的帮助,如果您需要更多信息,请告诉我.谢谢!

Any help is greatly appreciated, and if you need any more info please let me know. Thank you!

推荐答案

如果您的有效载荷是dict,请在请求库中使用json kwargs. data适用于字符串有效负载.在这里,您去了:

If your payload is a dict use json kwargs in requests lib. data works for string payload. Here you go:

r = requests.put(url, headers=headers, json=payload)

这篇关于Python-Spotify API返回错误400“格式错误的JSON";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆