Search code examples
instagraminstagram-apiinstagram-graph-api

How to collect Instagram App Secret to generate a long-lived token?


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.


Solution

  • 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.