【问题标题】:How to run cURL request using python如何使用 python 运行 cURL 请求
【发布时间】:2021-10-26 08:30:59
【问题描述】:

我想知道如何使用 python 运行以下 cURL 请求(我在 Jupyter notebook 中工作):

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}"

我看到一些类似的问题和答案建议使用“requests.get”,但我是一个完整的 python 新手,不知道如何构造整个请求的语法,包括 id、secret 和 token 元素。任何帮助将不胜感激。

谢谢!

【问题讨论】:

标签: python curl


【解决方案1】:

在此处立即将您的 Curl 请求转换为 Python: https://curl.trillworks.com/

【讨论】:

    【解决方案2】:

    无需使用 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)
    

    【讨论】:

    • @David23C 您是否将 a,b,c,d 替换为实际值?
    • 谢谢 - app_id 有错字 - 现在似乎可以正常工作了
    【解决方案3】:

    您可以使用一些在 python 中运行命令的库,例如 subprocess。例如:subprocess.run(["curl", "google.com"])

    【讨论】:

    • 我认为 OP 正在寻找类似 PHP 中的 Python cURL 库。
    • 我认为没有这种东西!
    • 有pycurl.io
    • 这是我在相关帖子 (stackoverflow.com/questions/26000336/…) 中看到的,但我仍然不确定如何使用“子进程”重写上面的代码。你能展示最终代码的外观吗?我有 app_id、秘密和访问令牌。
    猜你喜欢
    • 2016-07-18
    • 1970-01-01
    • 2013-01-11
    • 2016-11-04
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 2021-02-17
    • 2019-06-22
    相关资源
    最近更新 更多