PostMaster (Part 5) Integrating Instagram in mobile apps

Aaditya Doshi
Novumlogic
Published in
6 min readDec 3, 2021
Photo by Solen Feyissa on Unsplash

Welcome to the series of blog posts about PostMaster- A library for integrating social media accounts in mobile apps.

PostMaster library is a custom composer which enables its users to post on social media platforms such as Facebook, LinkedIn, Twitter, Instagram, and Telegram without opening and using the pre-installed app for each platform.

If you missed the previous parts of the blog series, you can find them here-

Part-1: Where I talked about how & why we decided to build PostMaster app, the problems we faced while building it, and tried and tested solutions for the same.

Part-2: Here, I talked in-depth about how to integrate Facebook in an android app and how we addressed the many issues faced while integrating the same.

Part-3: Here, I talked in-depth about how to integrate Twitter in an android app and how we addressed the many issues faced while integrating the same.

Part-4: Here, I talked in-depth about how to integrate LinkedIn in an android app and all the API integrations required to achieve the same

Instagram is one of the 5 social media platforms that we have integrated in the PostMaster library. So without any further ado let’s dive deep into how to integrate Instagram in mobile apps.

Key points before we start

  1. Instagram APIs are divided in 2 parts-
  • Instagram basic display API : For integrating basic Instagram login.
  • Instagram graph API : For accessing advanced features of an Instagram business account such as publishing content on user’s behalf, marketing campaigns, ads management etc.

2. Facebook’s developer API supports posting to Instagram business accounts only. Creator accounts, personal accounts are not supported.

3. Sharing to story is not supported for any account using the API.

4. Instagram business accounts must be linked to a Facebook page.

5. To get details of Instagram business account linked with Facebook page, Facebook login feature must be implemented in the app.

6. Only 25 post creation per day is supported using the API.

7. While posting images, maximum image size should be 8MB, aspect ratio must be within a range of 4:5 to 1.91:1, and minimum width supported is 320 and maximum is 1440.

Prerequisites

  • Facebook Developer Account
  • Facebook Login configured in the app with the required permission

If you are unaware of the above mentioned points then I highly recommend you to go through my blog of Facebook Integration where I have talked in detail on creating a developer account, Facebook Login, Facebook Permissions and all the related important details.

Permissions required

Permissions are crucial part while integrating Instagram in mobile apps because if permissions are not granted from the user while logging into Facebook then we can’t get the details of the user as well as the content posting rights on their behalf.

There are 3 main permissions required for accessing user’s details and for posting content on their behalf-

  • instagram_basic : For accessing the details of Instagram business accounts
  • instagram_publish_content : For publishing content to Instagram business accounts
  • manage_pages : For getting a list of pages that are linked to Instagram business accounts

Fetching the Instagram business accounts linked with Facebook Pages

After successful login with Facebook requesting all the permissions mentioned above, we will use the following code to call the API and fetch the Instagram accounts.

Instagram business accounts that are fetched using the API are stored in a Map<String, String> containing key value pair of account’s username and account id.

You can use this map to show a custom alert dialog with single selection or a bottom sheet to let user choose their suitable account for posting. Account id is very important for posting on user’s behalf so make sure you save it somewhere either locally or on cloud.

Facebook login flow to fetch Instagram accounts linked with Facebook pages
Left — Screens showing permission required to fetch Instagram business account details, Right — Image showing list of accounts linked with Facebook pages and providing user with option to choose account of their choice for posting.

Posting content to Instagram

Creating a post on Instagram requires an Image (compulsory) and a caption (optional). Creating a post on Instagram requires 3 steps-

Step 1 : Upload image to server and generate a public URL to access the image

In our case we have used Imgur : a public server to host your image for free.

For uploading image to Imgur, we will use the endpoint “/3/upload”.

Below is the details of the API and the parameters that needs to be passed to obtain the public URL for the image uploaded.

POST: https://api.imgur.com/3/upload

Parameters

It is recommended to use Multipart for uploading image to Imgur’s servers. Below mentioned code shows how to upload the image.

On successful uploading of image, we will receive a public URL that we will use to upload our image to Instagram in next step.

Step 2: Use the public link to upload image on Instagram and generate container id for the post.

Instagram images are stored in a container having unique id on Instagram servers and that container id is further used to create post on your profile. In this step we will upload the image to Instagram servers and generate a container id.

For uploading image to Instagram we will use the endpoint “/media”.

Below is the details of the API and parameters required.

POST: https://graph.facebook.com/{ig-user-id}/media

Here, {is-user-id} refers to the account id of the selected Instagram business account from the above steps.

Parameters

On successful attempt at the API, you will get response code 200 and json response containing id of the container. Parse the response and store that in a variable as this id will be used to create post in the next step.

Step 3: Use the container id from step-2 to finally create post to the business account.

After successful upload of the image to Instagram and getting the container id, next step is to create post to the business account using the container id obtained from the step above.

To create post we will use the endpoint “media_publish”.

Below are the details of the API and parameters required.

POST: https://graph.facebook.com/{is-user-id}/media_publish

Here, {is-user-id} refers to the account id of the selected Instagram business account from the above steps.

Parameters

On successful attempt at the API, you will get response code 200 indicating the creation of post to Instagram account.

Images showing post created on Instagram business account using above mentioned above (Screenshots taken from the app using PostMaster library)

Conclusion

We have covered all the aspects of integration with Instagram and all the related important points. Stay tuned for the upcoming blogs on other social media platform integrations.

Also do give claps if you find this useful in any capacity.

Happy Coding !

--

--

Aaditya Doshi
Novumlogic

Mobile Developer | Graphic Designer | Tech Enthusiast | Caffeine Driven | Avid Learner | Marvel Cinematic Universe & Harry Potter Fan | Blues & Blues rock lover