简体   繁体   中英

Python requests library for oauth token

So i was following the guide for getting a OAUTH token from https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/ I tried doing a python requests library of the above equivalent code but i got response 400 from the server. May I know where i am going wrong?

import requests
import json
import clientidandsecret


headers ={"Authorization": "Basic " + clientidandsecret.C_ID +":" +clientidandsecret.C_SECRET}
form = {"form":{"grant_type" :"client_credentials"}, "json":"true"}
result = requests.post("https://accounts.spotify.com/api/token", headers=headers, data=form)
print(result)

Your variable form is a Dict, if you want to use the parameter data in requests it needs to be a string, this will fix it:

import json
...
result = requests.post(url, headers=headers, data=json.dumps(form))

Or even better:

result = requests.post(url, headers=headers, json=form)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM