python - 如何将 CURL 转换为 Python 请求

标签 python curl python-requests

我目前正在尝试集成 Stripe Connect,并遇到了流动的 CURl POST 请求:

curl https://connect.stripe.com/oauth/token \
   -d client_secret=SECRET_CODE \
   -d code="{AUTHORIZATION_CODE}" \
   -d grant_type=authorization_code

但是我对 CURL 非常陌生,并且一直在做一些研究并尝试使用 requests 包来完成它。这就是我当前的代码:

data = '{"client_secret": "%s", "code": "%s", "grant_type": "authorization_code"}' % (SECRET_KEY, AUTHORIZATION_CODE) 
response = requests.post('https://connect.stripe.com/oauth/token', json=data)

然而,这总是返回响应代码 400。我不知道我哪里出了问题,任何指导将不胜感激。

最佳答案

该错误是因为您将 data 作为字符串传递,而不是 requests.post 调用的 json 参数期望它是 dict 。您的代码应该是:

import requests

data = {
     "client_secret": SECRET_KEY, 
     "code": AUTHORIZATION_CODE, 
     "grant_type": "authorization_code"
} 

response = requests.post('https://connect.stripe.com/oauth/token', json=data)

查看请求库的 More complicated POST requests 文档。

关于python - 如何将 CURL 转换为 Python 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48134662/

相关文章:

bash - curl -F 换行符未正确解释

python - 如何使用 Python Requests 库在 post 请求中发送 cookie?

python - Tkinter - 如何更改默认笔记本边框颜色?

python - Beautifulsoup HTML表格解析——只能获取到最后一行?

python - Matplotlib - 强制 3D 图使用可用的 figsize

bash - cURL 调用适用于数字但不适用于包含数字的变量

Python Pandas - 使用两列的标准计算平均值

bash - 在 bash 中从 txt 文件中多次读取(线程)

不使用代理的 Python 请求库

python - Flask request.args.get 没有获取所有参数(Python)