简体   繁体   English

如何使用 python 运行 cURL 请求

[英]How to run cURL request using python

I would like to know how to run the following cURL request using python (I'm working in Jupyter notebook):我想知道如何使用 python 运行以下 cURL 请求(我在 Jupyter 笔记本中工作):

curl -i -X GET "https://graph.facebook.com/{graph-api-version}/oauth/access_token?  
grant_type=fb_exchange_token&          
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={your-access-token}"

I've seen some similar questions and answers suggesting using "requests.get", but I am a complete python newbie and am not sure how to structure the syntax for whole request including the id, secret and token elements.我已经看到一些类似的问题和答案建议使用“requests.get”,但我是一个完整的 python 新手,我不确定如何构建整个请求的语法,包括 id、secret 和 token 元素。 Any help would be really appreciated.任何帮助将非常感激。

Thanks!谢谢!

no need to use curl. use the below无需使用 curl。使用下面的

import requests

graph_api_version = 'a'
app_id = 'b'
app_secret = 'c'
your_access_token = 'd'
url = f"https://graph.facebook.com/{graph_api_version}/oauth/access_token?grant_type=fb_exchange_token&client_id={app_id}&client_secret={app_secret}&fb_exchange_token={your_access_token}"
r = requests.get(url)

Convert your Curl request to Python instantly here: https://curl.trillworks.com/在此处立即将您的 Curl 请求转换为 Python: https://curl.trillworks.com/

you can use some lib that run commands in python like subprocess .您可以使用一些在 python 中运行命令的库,例如subprocess for example: subprocess.run(["curl", "google.com"])例如: subprocess.run(["curl", "google.com"])

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

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