def queue_song(session_id):
song_uri='spotify:track:5RwV8BvLfX5injfqYodke9'
tokens = get_user_tokens(session_id)
headers = {'Content-Type': 'application/json',
'Authorization': "Bearer " tokens.access_token,
}
url = BASE_URL 'player/queue'
data={
'uri':song_uri
}
response = requests.post(url,headers=headers,data=data).json()
print(response)
輸出:
{'error': {'status': 400, 'message': 'Required parameter uri missing'}}
https://developer.spotify.com/documentation/web-api/reference/#/operations/add-to-queue
我不認為身份驗證令牌有任何問題......因為“GET”請求作業正常
uj5u.com熱心網友回復:
默認情況下,使用data=inrequests.post()設定application/x-www-form-urlencoded使正文類似于 HTTP 表單請求的內容型別。
Spotify 的 API 是基于 JSON 的,因此您的資料需要是有效的 json 資料。
你可以通過兩種方式做到這一點:
response = requests.post(url,headers=headers,data=json.dumps(data)).json()
或者,更簡單地說:
response = requests.post(url,headers=headers,json=data).json()
這樣你就不需要手動設定application/json標題了。
編輯:瀏覽您鏈接的 API 檔案后,您撥打的電話有更多錯誤。
您正在發送引數data- 這是請求的正文。但是Spotify API 指定需要將引數放入Query即URI 的查詢字串中。這意味著您的請求應該是:
response = requests.post(url,headers=headers,params=data).json() # set query string not body
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/374199.html
標籤:Python 邮政 要求 发现 http-status-code-400
下一篇:獲取sql的值回傳php
