Python 提交请求。 Spotify API 提出格式错误的请求 Json

Python put request. Spotify API put request Malformed Json

非常感谢您的帮助。我正在尝试使用 Spotify API 将专辑添加到用户库中。我一直在与格式错误的 Json 有效载荷作斗争,并且完全没有想法。

这是我目前的简化版本

url = 'https://api.spotify.com/v1/me/albums'
payload = {'body': ['01kTgTBiZkCFY3ZH2hBH6u', '4sz6Fn4BYORRLIc1AvQwQx']}
headers = {'Authorization':'Bearer {}'.format(access_token), 'Content-Type':'application/json',}
response = requests.put(url,headers=headers, data=payload)
print(response.json())

我收到的错误在 json 响应中:

{'error': {'status': 400, 'message': 'Malformed json payload'}}

我已尝试按照以下方式更改 requests.put,但所有尝试都返回相同的错误

response = requests.put(url,headers=headers, json=payload)
response = requests.put(url,headers=headers, data=json.dumps(payload))
response = requests.put(url,headers=headers, json=json.dumps(payload))

07bYtmE3bPsLB6ZbmmFi8d:此 spotify id 用于专辑 Dancefloor Hits #1。我检查了我的 spotify 帐户,它在我帐户的相册中。下面是我对 运行 的代码。

import requests

url = "https://api.spotify.com/v1/me/albums"

payload = {"ids": "27cZdqrQiKt3IT00338dws"}
headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer %s' % (access_token) # paste your access token in here
}

''' you can use the same syntax as above but the key was that your ID of album had to be a **parameter** not ***data***. 
To do that you use the params kwarg'''

response = requests.request("PUT", url, headers=headers, params = payload)

print(response.status_code) # should print out 200 when you run the code
# shows whether status was valid