【问题标题】:Send data in body via POST - Flask通过 POST 在正文中发送数据 - Flask
【发布时间】:2021-02-02 01:25:34
【问题描述】:

我需要使用 Flask 向 Instagram Basic API 发送 POST 请求。

这是我的代码:

def get_token_insta(code):
    dict_to_send = {"client_id": "XXX",
                    "client_secret": "XXX",
                    "grant_type": "authorization_code",
                    "redirect_uri": "XXX",
                    "code": "XXX"}
    json_string = json.dumps(dict_to_send)
    res = requests.post('https://api.instagram.com/oauth/access_token', data=json_string)
    data = res.json()
    print(data)
    # More code...

代码运行良好,但 Instagram 的响应是

{ "error_type": "OAuthException", "code": 400, "error_message": "缺少必填字段 client_id" }

阅读 official documentation 和 @murp answer 我需要将数据放入 POST 请求的正文中。

我错了吗?我怎样才能做到这一点?

【问题讨论】:

    标签: python python-requests instagram-api


    【解决方案1】:

    你在哪里发现需要将POST请求参数序列化为json?当然,您会得到 JSON 响应,但您不需要序列化请求参数。 尝试将dict_to_send 本身作为data 参数传递:

    requests.post(url, ..., data=dict_to_send)
    

    更多信息在requests documentation

    【讨论】:

    • 非常感谢,我在想 JSON 是必需的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多