尝试使用所有必需的参数获取Instagram访问令牌,但仍收到错误代码400 [英] Try to fetch Instagram access token with all required params but still get an error code 400

查看:97
本文介绍了尝试使用所有必需的参数获取Instagram访问令牌,但仍收到错误代码400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是服务器端代码,试图向Instagram发出POST请求以获取访问令牌

app.get('/instagram/json', (req, res) => {
    axios({
        method: 'post',
        url: 'https://api.instagram.com/oauth/access_token',
        data: {
            'client_id': instagramClientId,
            'client_secret': instagramClientSecret,
            'grant_type': 'authorization_code',
            'redirect_uri': 'http://localhost:3000',
            'code': instagramCode
        }
      }).then((response) => {
            console.log(response);
        }).catch((e) => {
              console.log(e);
          });
});

这是我收到的错误响应.它告诉我尽管我已经明确提供了"client_id",但仍是必需的.

 data: 
  { error_type: 'OAuthException',
    code: 400,
    error_message: 'You must provide a client_id' } } }

推荐答案

后来,但是由于网络上没有很好的答案,所以花了我一段时间:

Late, but since there's not a good answer on the web and took me a while:

在将其提供给fetch-node的帖子之前,您必须将您的身体包裹在一个form-data中.

You'd have to wrap your body inside a form-data before providing it to fetch-node's post.

安装表格数据:npm install --save form-data 然后:

Install form-data: npm install --save form-data then:

const formData = require("form-data");
const insta_form = new formData();
insta_form.append("client_id", your client id);
insta_form.append("client_secret", your client secret);
insta_form.append("grant_type", "authorization_code");
insta_form.append("redirect_uri", your redirect uri);
insta_form.append("code", user code);
const shortTokenRes = await fetch(
  "https://api.instagram.com/oauth/access_token",
  {
    method: "POST",
    body: insta_form,
  }
);

这篇关于尝试使用所有必需的参数获取Instagram访问令牌,但仍收到错误代码400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆