Search code examples
facebookgoogle-apps-scriptfacebook-graph-apiinstagramfacebook-access-token

Generate non-expiring token for Facebook Graph API to publish on Instagram Business Account via Google Apps Script


To post photos on instagram using Google Apps Script, I follow these steps!

To generate a short-lived token I access the Graph API Explorer, add these scopes:

pages_show_list
instagram_basic
instagram_manage_comments
instagram_manage_insights
instagram_content_publish
pages_read_engagement
pages_manage_posts
public_profile

I click on generate access token, after that I go to the Access Token Debugger, put the generated token, scroll to the bottom of the page and click on extend access token.

With this long-lived token I add to my code and post images on my Instagram Business Account:

function instapost(url_photo,txt_subtitle) {
  const access_token = 'XXXXXXXXXXXXX';
  const instagram_business_account = '123456789';
  const image = url_photo;
  const text = txt_subtitle;
  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);
}

The problem with this is that every 2 months I have to go back and do these manual steps again.

So I tried to generate the short-lived token directly by a direct url call:

https://developers.facebook.com/docs/facebook-login/guides/access-tokens#usertokens

Using this values: enter image description here

Like this:

const app_id = ...;
const app_secret = ...;
const url = 'https://graph.facebook.com/oauth/access_token' + 
  '?client_id=' + app_id + 
    '&client_secret=' + app_secret + 
      '&grant_type=client_credentials' +
        '&scope=pages_show_list,instagram_basic,instagram_manage_comments,instagram_manage_insights,instagram_content_publish,pages_read_engagement,pages_manage_posts,public_profile'

UrlFetchApp.fetch(url)

But when going through the debugger, the only existing scope is the public_profile.

Is there any way to get this token with all the necessary scopes without having to do it manually?

I've done a lot of research and I always come back to the same problem, because normally the use is aimed at customers, so they will always login and approve access, generating the scopes, but in my case I won't be logging in on the screen, I just want to automate the posts I make on my Instagram.

Additional information:

There are several questions/answears:
https://stackoverflow.com/a/28418469/11462274
https://stackoverflow.com/a/21927690/11462274
https://stackoverflow.com/a/58878246/11462274

About generating an eternal token, in fact there is a way to do it, but the problem is that even though it is eternal, it has a time limit of 2 months for data access, so anyway after 2 months it becomes useless for me, because if he can't access the data, he won't be able to publish.

And as I don't have a company to link to, it becomes unfeasible to try to generate from Meta Business Suite, it's not possible that this is the only way since access to the token is so simple through Graph Explorer.

Using https://business.facebook.com/settings/system-users/, an eternal token is created, but even checking all the scopes released to mark when generating the token, it only returns these authorizations:

enter image description here


Solution

  • I'm going to do a complete and definitive step-by-step to solve this problem in relation to posting photos on Instagram with access tokens from the Facebook Graph API.

    Assuming that you already have an application created here:
    https://developers.facebook.com/apps/

    With this products mark:

    enter image description here

    and linked to your account, access this page:

    https://business.facebook.com/settings/system-users/

    There, you will add a new System User but note that there is a very important detail, add it as Administrator Access!

    After that, you MUST access the Applications tab on the left menu, choose your already created application and click Add People.

    Select the Admin User you created and check all the existing boxes, which are:

    Develop Application
    see insights
    Test Application
    Manage Application

    After registering this user with these accesses, go back to the System Users page, click on Generate new token and check all the necessary scopes that are even at the beginning of my question.

    As I already want to prevent future necessary scopes, I marked them all. When debugging the token now here it is, freed forever and with all necessary scopes:

    enter image description here