Python3中的curl等效post请求
我必须用Python3编写以下curl脚本Python3中的curl等效post请求,python,http,curl,python-requests,Python,Http,Curl,Python Requests,我必须用Python3编写以下curl脚本 curl -X POST\ -u '$client_id:$client_secret'\ -d 'grant_type=client_credentials&access_lifetime=7200' https://www.example.com/oauth/token 我试过这么做 data = {'grant_type': 'client_credentials', 'access_lifetime': '7200'} respon
curl -X POST\
-u '$client_id:$client_secret'\
-d 'grant_type=client_credentials&access_lifetime=7200'
https://www.example.com/oauth/token
我试过这么做
data = {'grant_type': 'client_credentials', 'access_lifetime': '7200'}
response_result = requests.post(
'https://www.example.com/oauth/token',
data=json.dumps(data),
auth=('client_id', 'client_secret)
)
但是在print(response\u result.text)
上,由于请求中未指定授予类型,因此会出现错误
我认为数据
没有传递,或者我使用了错误的方式传递数据
如何在请求中传递数据
?您需要向数据
关键字参数传递字典,而不是JSON字符串:
data = {'grant_type': 'client_credentials', 'access_lifetime': '7200'}
response_result = requests.post(
'https://www.example.com/oauth/token',
data=data,
auth=('client_id', 'client_secret)
)
有关详细信息,请参阅。数据=数据
而不是
data=json.dumps(数据)