【问题标题】:how to convert the instagram curl post request to javascript request?如何将 instagram curl 发布请求转换为 javascript 请求?
【发布时间】:2022-01-08 05:57:33
【问题描述】:

我正在尝试将我从 Instagram 获得的代码更改为令牌。在 Instagram DOCS 中,它是用 curl 请求编写的,我想使用 JavaScript。 它看起来像这样:

curl -X POST \
  https://api.instagram.com/oauth/access_token \
  -F client_id={app-id} \
  -F client_secret={app-secret} \
  -F grant_type=authorization_code \
  -F redirect_uri={redirect-uri} \
  -F code={code}

我试图了解-F 是什么以及用什么符号来替换它。

任何解决方案都会有所帮助,谢谢。

【问题讨论】:

    标签: javascript ajax api curl instagram-api


    【解决方案1】:

    -F 用于模拟 html 表单提交 https://curl.se/docs/manpage.html#-F

    如果您想使用 JavaScript fetch() API 发送请求,请将所有这些参数设置为请求正文并将内容类型设置为 application/x-www-form-urlencoded

    如果您以交互方式发送这些请求, 或者,您可以创建一个带有表单和提交按钮的 HTML 页面。(例如https://www.w3schools.com/tags/att_form_enctype.asp

    【讨论】:

    • 非常感谢!但请求不完整。当我尝试打印我得到的数据时:'缺少必填字段 client_id'。这是我的代码:fetch("https://api.instagram.com/oauth/access_token", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", }, body: JSON.stringify({client_id:{app-id}, client_secret:{app-secret}, grant_type:authorization_code, redirect_uri:{redirect-uri}, code:{code}}), }) .then((res) => res.json()) .then((data) => console.log("data", data));,它在邮递员中工作。
    • 你不应该使用 JSON.stringify()。您没有从 at 发送 JSON。您需要以 application/x-www-form-urlencoded 格式发送它们。例如client_id={app-id} .... 查看邮递员请求的正文
    猜你喜欢
    • 2020-01-26
    • 2021-03-17
    • 2020-02-26
    • 2019-06-28
    • 2021-11-14
    • 2018-08-29
    • 2018-04-16
    • 2021-02-06
    • 2017-01-13
    相关资源
    最近更新 更多