Scraping Instagram with Google Script

I thought I’d take a stab at using Google Script to capture Instagram data after being inspired by all the great Martin Hawksey has done with the TAGS Explorer. I’m doing something very similar and it turned out to be fairly straightforward to take the PHP I’d written previously and turn it into something that’d function in Google Sheets. I found this example highly useful in creating my own script.1

The following script can also be set with a time based trigger to fire every X minutes/hours/days which is perfect for this particular project.

Google Script Trigger

You can see it pulling 20 pictures worth of data every hour here if you’d like.

function addTitles() {
  var sheet = SpreadsheetApp.getActiveSheet();
  // sets up the header row for the sheet
  sheet.appendRow(['User Name', 'Likes Count', 'Comment Count', 'URL', 'Caption', 'Filter', 'Created Time']);

function getInstagramData(searchTerm) {
	var search = 'vape'; // search term
	var url = '' + search +'/media/recent?client_id=XXXXXXXXXXXXXXXXXX'; // replace this with your API client ID
	var  result = UrlFetchApp.fetch(url);
	if (result.getResponseCode() == 200) {
		var json = JSON.parse(result.getContentText());
		var grams =;

		// Instagram gives you 20 on the first request
		for (var i = 0; i<=20; i++) {

function logGram_(gram) {
	var log = [];
// pushes the various elements into the spreadsheet	

1 I basically copied portions of it.

Comments on this post

  1. Paige Morgan said on August 11, 2015 at 12:50 pm

    This looks really useful — but did you need to create a spreadsheet for it to push the data to? I’m assuming so, but I don’t see any info about configuring it in your instructions.


    • Tom Woodward said on August 11, 2015 at 1:04 pm

      You’re right. Just make a new spreadsheet and add this script via the script editor. Then run it and set your trigger for how often it should repeat.

      • Paige Morgan said on August 11, 2015 at 2:23 pm

        Thanks! That worked perfectly.

  2. sandrojazzar said on September 9, 2015 at 3:07 am

    Thanks a lot for building and sharing this! 🙂

    But what about getting more than 20 pics? Is there a way to do that?
    Also, I get this error when I run the function (results still do appear though!) :

    ReferenceError: “logGram_” is not defined. (line 22, file “”)

    • Tom Woodward said on September 10, 2015 at 3:11 pm

      Here’s how I did that with PHP. This version fetches 100 entries. If you don’t have access to a server, you can run it on something virtual like Vagrant.

      I meant to cycle back around and add that to the Google version but haven’t gotten to it yet.

      I don’t really understand those errors either. Everything seems to work just fine and the elements that referenced in my errors are there . . . For now, I just delete the emails. Sad but true.

      It’s also worth noting that I only vaguely know what I’m doing. 🙂

    • Tom Woodward said on September 15, 2015 at 8:14 pm

      Here’s a version that’ll grab 40. You can probably see how to make it grab 60 or 80 at a time pretty easily. It’s not pretty code but it’s pretty straight forward. I may make it loop eventually and make something nicer for users but we’ll see what time allows.

  3. Cris Wilson said on May 14, 2016 at 11:22 am

    I get an error any time I try to run this. Is this script still valid with the new API changes to IG? Request failed for CLIENT ID REMOVED HERE) returned code 400. Truncated server response: {“meta”: {“error_type”: “OAuthAccessTokenException”, “code”: 400, “error_message”: “The access_token provided is invalid.”}} (use muteHttpExceptions option to examine full response) (line 16, file “Code”)

    • Tom Woodward said on May 14, 2016 at 2:57 pm

      That URL works with my client ID. It may be that they aren’t allowing it on newer API registrations.

      Were I doing this again, and I might in the near future, I’d do the full oauth and probably use this.

  4. EDH said on September 7, 2017 at 3:26 am

    Does this still work?

  5. Monkey Coco said on November 23, 2017 at 3:35 am

    Don’t work 🙁 same error here.

    Request failed for CLIENT ID) returned code 400. Truncated server response: {“meta”: {“code”: 400, “error_type”: “OAuthAccessTokenException”, “error_message”: “The access_token provided is invalid.”}} (use muteHttpExceptions option to examine full response) (line 12, file “Code”)

    • Tom Woodward said on November 23, 2017 at 6:35 pm

      I’d expect it’s no longer an option. It’s been nearly two years and Instagram’s new API is pretty locked down.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Trackbacks and Pingbacks on this post

  1. Re-Scraping Instagram – Bionic Teaching said on September 15, 2016 at 9:30 am

    […] when Instagram’s API rules didn’t completely suck, I wrote a few posts on scraping it so that some of our faculty could use those data in their […]

TrackBack URL