Python3中的curl等效post请求

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

我必须用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'}
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(数据)