简体   繁体   English

用于 oauth 令牌的 Python 请求库

[英]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.因此,我按照从https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/获取 OAUTH 令牌的指南进行操作,我尝试使用上述等效代码的 python 请求库,但我得到了响应400 来自服务器。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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