简体   繁体   English

python 3中的curl等效发布请求

[英]curl equivalent post request in python 3

I have to write following curl script in python 3 我必须在python 3中编写以下curl脚本

curl -X POST\
-u '$client_id:$client_secret'\
-d 'grant_type=client_credentials&access_lifetime=7200'
 https://www.example.com/oauth/token

I tried doing this 我尝试这样做

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)
)

But on print(response_result.text) it gives error as The grant type was not specified in the request 但是在print(response_result.text)却给出错误,因为The grant type was not specified in the request

I think the data is not passing or I'm using wrong way to pass data . 我认为data没有传递,或者我使用错误的方式传递data

How to pass data in the requests ? 如何在requests传递数据?

You need to pass a dictionary to the data keyword argument, not a JSON string: 您需要将字典传递给data关键字参数,而不是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)
)

See the documentation for details. 有关详细信息,请参见文档

data=data 数据=数据

Instead of 代替

data=json.dumps(data) 数据= json.dumps(数据)

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

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