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
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: