Python 放置请求.Spotify API 放置请求格式错误的 Json [英] Python put request. Spotify API put request Malformed Json
问题描述
我真的很感谢这里的帮助.我正在尝试使用 Spotify API 将专辑添加到用户库.我一直在为格式错误的 Json 有效负载而苦苦挣扎,并且完全没有想法.
I would really appreciate some help here. I am trying to use the Spotify API to add albums to a users library. I have been struggling with a Malformed Json Payload, and am completely out of ideas.
这是我目前所坐的简化版本
Here is a simplified version of what I am currently sitting on
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 响应中:
The error I am receiving is in the json response:
{'error': {'status': 400, 'message': 'Malformed json payload'}}
我已尝试按照以下方式更改 requests.put,但所有尝试都返回相同的错误
I have tried changing requests.put as per below, but all attempts are returning the same error
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 帐户,它在我帐户的相册中.下面是我运行它的代码.
07bYtmE3bPsLB6ZbmmFi8d
: This spotify id is for the album, Dancefloor Hits #1. I checked my spotify acct and there it was in my Albums on my acct. Below is my code to run that.
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
这篇关于Python 放置请求.Spotify API 放置请求格式错误的 Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!