3

I am trying to set up a chron job to connect to the instagram API, grab 'my feed' and download the images. I cannot get past the OAuth 2.0 step. I have already looked at a number of resources including:

How to authorize with oauth 2.0 from appscript to Google APIs? - methods are deprecated and I cannot get the pop up to for the oauth to show up.

https://code.google.com/p/google-apps-script-issues/issues/detail?id=2580 and all the links that follow in the discussion. I cannot figure out how to apply this to work without an html page.

http://www.googleappsscript.org/home/downloading-instagram-photos-to-your-google-drive-using-google-apps-script works well for hash tags, but I would like to be able to get the feed from my user account.

Any help would be greatly appreciated, this is the best I have been able to figure out, if I could get the pop up to work I would be good to go, but I cannot.

function startInstagram () {
    var redurl = getCallbackURL(getInstagram);
    var consumerKey = '#######';
    var consumerSecret = '#######';
    var parameters = {
        method : 'post',
        payload:   
'grant_type=authorization_code'+'&client_id='+consumerKey+'&client_secret='+consumerSecret+'&grant_type=authorization_code&redirect_uri='+redurl+'&response_type=token'
       };
      var token = UrlFetchApp.fetch('https://api.instagram.com/oauth/authorize/', parameters).getContentText();
      Logger.log(['token', token]);
    }

function getInstagram (vars) {
  var res = {};
  Logger.log(['get', vars]);
  return;
}
function getCallbackURL(callbackFunction) {
   var scriptUrl = 'https://script.google.com/d/<ID>';
   var urlSuffix = '/usercallback?state=';
   var stateToken = ScriptApp.newStateToken()
       .withMethod(callbackFunction)
       .withTimeout(60*10*5)
       .createToken();
  return scriptUrl + urlSuffix + stateToken;
 }
Community
  • 1
  • 1
aduss
  • 556
  • 3
  • 12

1 Answers1

2

There is a GitHub repository that describes a library for using OAuth2 in Apps Script. In its README file, it shows how to use the library with Drive API as an example. If you substitute the Instagram API for Drive in the example code, you should be close to what you need.

The Instagram API Authorization docs covers most of what you'll need to do to get an access token -- the steps parallel the instructions in the GitHub library.

You'll need to make sure your script is a registered application with Instagram so you can get the client ID and client secret and so you can set the redirect URI, which will be of the form

https://script.google.com/macros/d/{PROJECT KEY}/usercallback

for Apps Scripts.

If you are only interested in downloading your photos, the basic scope permissions (granted by default) should be sufficient.

Once you have a valid access token, you should be able to make requests from the Instagram API using UrlFetchApp.fetch().

Ryan Roth
  • 1,394
  • 9
  • 15