skip to Main Content

On this page I generate the access token and with it I can publish the image on my Instagram:

enter image description here

For publish:

function InstagramPost() {
  const access_token = 'GENERATE ACESS TOKEN';
  const instagram_business_account = 'YYYYYYYYYYYYYYYYYY';
  
  const image = 'https://upload.wikimedia.org/wikipedia/en/9/95/Test_image.jpg';
  const text = 'Hello World';
  var formData = {
    'image_url': image,
    'caption': text,
    'access_token': access_token
  };
  var options = {
    'method' : 'post',
    'payload' : formData
  };
  const container = 'https://graph.facebook.com/v14.0/' + instagram_business_account + '/media';

  const response = UrlFetchApp.fetch(container, options);

  const creation = response.getContentText();
  var data = JSON.parse(creation);
  var creationId = data.id
  var formDataPublish = {
      'creation_id': creationId,
      'access_token': access_token
  };
  var optionsPublish = {
    'method' : 'post',
    'payload' : formDataPublish
  };
  const sendinstagram = 'https://graph.facebook.com/v14.0/' + instagram_business_account + '/media_publish';
  
  UrlFetchApp.fetch(sendinstagram, optionsPublish);
}

Now I want to take this access token and generate a long-lived one with it!

It asks for Instagram App Secret, but that path indicated (App Dashboard > Products > Instagram > Basic Display > Instagram App Secret) doesn’t exist in App Dashboard!

enter image description here

I tried using the App secret as a parameter:

enter image description here

"https://graph.instagram.com/access_token
  ?grant_type=ig_exchange_token
  &client_secret={App Secret Key}
  &access_token={short-lived-access-token}"

But this error occurs:

Sorry, this content isn't available right now

The Facebook API is 100% accessible, so that’s not the problem.

2

Answers


  1. Chosen as BEST ANSWER

    There is a difference in approach between Basic Display and Instagram Graph API for Business Account.

    So the way to convert a short-lived token to a long-lived token for Instagram Business Account is:

    "https://graph.facebook.com/{graph-api-version}/oauth/access_token?  
        grant_type=fb_exchange_token&          
        client_id={app-id}&
        client_secret={app-secret}&
        fb_exchange_token={your-short-lived-access-token}"
    

    Note that Instagram App Secret is not used, instead, use App Id and App Secret.


  2. In Javascript/NodeJS I couldn’t get it working at all (also on PostMan), I was using the request library.

    Changed my code to:

    const respLong = await axios.get(`https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret=${CLIENT_SECRET}&access_token=${accessTokenShort.toString()}`);
    

    And magically this works. I can’t tell you why what seems to be the exact same request in Postman and the request library doesn’t work.

    See pic of the url to get app secret (add your app ID) is: https://developers.facebook.com/apps/YOUR_APP_ID(number)/instagram-basic-display/basic-display/
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search