Integration: Connect & Manage Instagram Accounts via Facebook Graph API

I recently had a chance to integrate Facebook Graph API to connect Instagram (IG) accounts in order to manage IG accounts. For Business managers it is very important that they have tools, proper interface to manage the IG accounts easily.

So before we dive in, please understand there are some limitations to use the API, you will have to go through the review process and will have to verify your business by providing proper documentation. Official API for IG no longer accepts app requests for review. So Facebook Graph API is the only way to go with now.

Limitations

  • The API cannot be used to access non-Business or non-Creator Instagram accounts. If you are building apps for non-Business or non-Creator Instagram users, use the Instagram Platform API instead.
  • Currently, Business Discovery only returns data about Instagram Business Users.
  • Content Publishing can only be used on behalf of Instagram Business Users.

Permissions

There are multiple permissions to look at based on use case. I will be using instagram_basic to get metadata. Here are all the permissions graph API offers:

Getting Started

You will need access to the following:

Now right to the steps to integrate Graph API.

  1. Setup a basic project, for this article a PHP project.
  2. Now Install Official Facebook SDK. I created everything in PHP so link to Facebook’s PHP SDK is https://github.com/facebook/php-graph-sdk. You can use composer to install it.
  3. Once installed, download following wrapper I have created to make it easier to make calls to graph API and handle request and response. Once downloaded, include it in the project. Make sure to fill in app ID, app secret and redirect url in it. https://gist.github.com/abhij89/f409b4ae91bfc228cb66c78a1b969131
  4. Now create a file, and put following content in it:
    <?php
    include('InstagramGraphLogin.php');
    $instagramLogin = new InstagramSocial();
    $instagramLoginUrl = $instagramLogin->getLoginUrl(); // include the wrapper
    ?>
    <a href="<?php echo $instagramLoginUrl; ?>" class="btn btn-social btn-instagram">Connect Instagram</a> <!-- Login Authentication URL for Facebook -->


    This will put a simple button which will take you to Facebook for login authentication and asking you to grant permissions to our app to manage Instagram Accounts.
    Note: Instagram account should be connected to one of the Facebook page for this to work. Go to your Facebook page settings, click on Instagram from sidebar and connect IG account. Following image shows how:
  5. Create another file which will handle the response once user authenticates our app. Put following content in the same:
    <?php
    include('InstagramGraphLogin.php');
    $instagramLogin = new InstagramSocial();
    $connectedAccountsData = $instagramSocial->getUserInfo(); // returns basic metadata for connected IG accounts

    echo "<pre>"; print_r( $connectedAccountsData );
    // Do something with the data

  6. You’re done.

Easy enough.

Here is a official documentation from Facebook: https://developers.facebook.com/docs/instagram-api/getting-started

Issue I ran in while working it out: https://github.com/facebook/php-graph-sdk/issues/1126

Author’s GitHub Profile: https://github.com/abhij89