PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 26, 2022

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

 October 26, 2022     instagram, instagram-api, instagram-graph-api     No comments   

Issue

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.



Answered By - Digital Farmer
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing