2

I'm using fetch to retrieve data from Instagram API for authorization

Need access token in response to the request.

But, got error : {"error_type": "OAuthException", "code": 400, "error_message": "You must provide a client_id"}

const url = 'https://api.instagram.com/oauth/access_token';

let fetchData = {
            method: 'POST', 
            body: JSON.stringify({
              'client_id': 'xxxxxxxxxxxxxxxxxxxxxxx',
              'client_secret': 'xxxxxxxxxxxxxxxxxxxxxxx',
              'grant_type': 'authorization_code',
              'redirect_uri':'http://localhost:3000/',
              'code':'xxxxxxxxxxxxxxxxxxxxx'
            })
}

fetch(url, fetchData)
        .then((response) => {
          console.log(response, "::response")
          return response.json();
        }).catch((error) => {
          console.log(error," :: ERROR");
          return error;
        });
Kaleem Elahi
  • 316
  • 2
  • 14
  • 3
    The Instagram API requires you to add the data in the URL as query parameters, like: `https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code` See here: https://www.instagram.com/developer/authentication/ – Miguel Calderón Feb 14 '18 at 10:13
  • @Miguel, As i'm using Server-side (Explicit) Flow.. i got that error! or Pls, Suggest that Should I use client(implicit) Flow as u commented above for react ? ..Thanks – Kaleem Elahi Feb 14 '18 at 11:26
  • In Server-side flow you have to use the URL like that. I quote: "Server-side (Explicit) Flow. Using the server-side flow is quite easy. Simply follow these steps: Step One: Direct your user to our authorization URL `https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code` – Miguel Calderón Feb 14 '18 at 12:29
  • So, should i use the url in `fetch(url).then(...)` ? @Miguel – Kaleem Elahi Feb 14 '18 at 13:04
  • The docs say you have to do that first, yes, and then do another fetch. But you have to use a public redirect_uri hook in order ot receive the code. I recommend you to read through the Instagram API docs and rewrite your code once you fully understand the authorization flow. – Miguel Calderón Feb 14 '18 at 13:26
  • Can't use Server side(Explicit) because i'm using react... & The (Implicit) way working just fine.. Thanks . @Miguel – Kaleem Elahi Feb 15 '18 at 14:02
  • Great that you got it sorted out. – Miguel Calderón Feb 16 '18 at 09:08

0 Answers0