1

I'm trying to call an Instagram API call from my server (nodeJS) and I am not sure how to do so.

curl -X POST https://api.instagram.com/oauth/access_token \
  -F client_id=123456... \
  -F client_secret=123abc... \
  -F grant_type=authorization_code \
  -F redirect_uri=https://google.sg/ \
  -F code=123abc

This command returns the desired output when I run it on the command line, but I can't seem to find a way to do the same in JavaScript. Here is what I've tried:

axios
  .post("https://api.instagram.com/oauth/access_token", {
    client_id: 123456...,
    client_secret: "123abc...",
    grant_type: "authorization_code",
    redirect_uri: "https://google.sg",
    code:
      "123abc...",
  })

This is what is logged in by the catch block:

data: {
  error_type: 'OAuthException',
  code: 400,
  error_message: 'Missing required field client_id'
}

I think -F refers to form data, but I can't seem to find a way to do that in axios.

edit: I've tried it with just client_id, and it returns the same error.

edit 2: I don't think this is a fix but Postman Agent has a nifty function to translate the HTTP request into code: enter image description here

It can be accessed via Code on the right.

clueless waffle
  • 381
  • 3
  • 10

1 Answers1

0

Just see that in your CURL request you have the redirect_uri: https://google.sg/

And in your axios params you have: https://google.sg

You are missing a / in the axios parameter.

The way Oauth2 works is that it appends parameters to the redirect_uri specified in your api settings, so if you set it to https://google.sg/, it is expected that it is indeed the same url, without any changes.

The response you should be getting would be: https://google.sg/access_token=#xxxx

As you had specified, the response would be different, like so:

https://google.sg?access_token=#xxxx

That is not only a non-valid URL, but it also doesn't match the one you specified.

For this reason it is recommended that you set a standard path, for example:

https://google.sg/oauth

So in the end you would get:

https://google.sg/oauth?access_token=#xxxx