Before

After

We'll be using the new Instagram API within the Facebook Marketing API reference. Read all about the Instagram Graph API here.

This tutorial will guide you through the steps of exporting the posts from your Instagram business account feed.

To get started, make a copy of the above Google Sheet and open your script editor by going to "Tools" -> "Script editor". It will have the caption, likes, comments and image so that it becomes much easier to analyse why certain posts perform better than others.

Step 1 - Create a Facebook access token

To get an access token, you first have the create a Facebook app. You can do this right here.

From there on head over to the Graph API explorer to generate an access token. Make sure to give your token the following permissions:
  • instagram_basic
  • pages_show_lists

Step 2 - Increase the lifespan of the access token

Standard access tokens are only valid for around 30 minutes, which isn't too handy if you want to use this script many times.

Luckily you can quite easily extend that duration via the access token debugger.  Open the debugger, paste in your access token, and extend its expiry date. It should now last at least a few months, but it might still be good to set up a reminder somewhere.

Step 3 - Request account IDs

After you've copied your long-lived access token into the sheet we can run the script. Firstly, we need to figure out to which account you've got access. We do that with the /me/accounts request.

var apiUrlGetAccounts = 'https://graph.facebook.com/v7.0/me/accounts?access_token=' + accessToken; 
var responseApiGetAccounts = UrlFetchApp.fetch(apiUrlGetAccounts);
var result = JSON.parse(responseApiGetAccounts.getContentText());

Step 4 - Request Instagram business account IDs

Once we have your account ID, we can find the Instagram business account ID. However, this will only work if you've already linked your Facebook and Instagram accounts together.

var apiUrl = 'https://graph.facebook.com/v7.0/'+ PageID +'?fields=instagram_business_account&access_token=' + accessToken; 
var responseApiGetBusinessAccount = UrlFetchApp.fetch(apiUrl);
var resultBusinessAccount = JSON.parse(responseApiGetBusinessAccount.getContentText());

Step 5 - Request Instagram business account posts

Now that we have the Instagram business account ID, we can make a fetch for the posts.

var apiUrl = 'https://graph.facebook.com/v7.0/'+ BusinessID +'/media?limit=1000&access_token=' + accessToken; 
var responseApiGetMedia = UrlFetchApp.fetch(apiUrl);
var mediaResult = JSON.parse(responseApiGetMedia.getContentText());

Step 6 - Request post details

Once we have all the post IDs, we're just one step away from getting all the individual post details.

// define empty array to insert media details
var caption = [];
var commentsCount = [];
var like_count = [];
var media_url = [];
var media_type = [];
var is_comment_enabled = [];
var permalink = [];
var timestamp = [];
var username = [];
    
//loop through all posts and get details  
for (var i=0; i<media.length; i++){
      
  //define post 
  var MediaID = media[i];   
      
  //get details for that post
  var apiUrl = 'https://graph.facebook.com/v7.0/'+ MediaID +'?fields=comments,caption,comments_count,like_count,media_url,media_type,id,ig_id,is_comment_enabled,owner,permalink,shortcode,thumbnail_url,timestamp,username&access_token=' + accessToken; 
      
  var responseApiComments = UrlFetchApp.fetch(apiUrl);
  var result = JSON.parse(responseApiComments.getContentText());
      
  //push post details to the array
  caption.push([result.caption]); 
  commentsCount.push([result.comments_count]); 
  like_count.push([result.like_count]); 
  media_url.push([result.media_url]); 
  media_type.push([result.media_type]); 
  is_comment_enabled.push([result.is_comment_enabled]); 
  permalink.push([result.permalink]); 
  timestamp.push([result.timestamp]); 
  username.push([result.username]); 
}

Step 7 - Clean sheet and insert values

Now that we've extracted all the feed details and pushed them into the associated arrays, we're only left with inserting the arrays into the sheet. To do so, we first clean the sheet, define the input ranges, and then set the values.

// remove prior data from the sheet in range A13:D & F:G 
var sheetRange = sheet.getRange("F5:N").clear();
  
// import the posts with details into the sheet
sheet.getRange(5, 6, caption.length, 1).setValues(caption)
sheet.getRange(5, 7, commentsCount.length, 1).setValues(commentsCount)
sheet.getRange(5, 8, like_count.length, 1).setValues(like_count)
sheet.getRange(5, 9, timestamp.length, 1).setValues(timestamp)
sheet.getRange(5, 10, media_url.length, 1).setValues(media_url)
sheet.getRange(5, 11, media_type.length, 1).setValues(media_type)
sheet.getRange(5, 12, is_comment_enabled.length, 1).setValues(is_comment_enabled)
sheet.getRange(5, 13, permalink.length, 1).setValues(permalink)
sheet.getRange(5, 14, username.length, 1).setValues(username)

It took three requests making this script look pretty tough, but by now you should be able to see your feed's details after running the script.


Let me know if this was useful & let's connect on     or