Instagram Graph API using Python

Ritik khandelwal
5 min readSep 22, 2022

--

This blog will show how to implement Facebook Graph API using python.

Facebook Graph API gives you the power to get user analytics, post analytics, and the ability to post images, videos, and reels. But in this blog, we will understand how to get the long_lived_access_token and get all the posts of the Instagram user.

  1. We have to log into the Facebook developer account and create an app
Go to the Facebook developer site and log in.

2. Then we have to create an App. In this blog, I will select my app type as None, but if you want to select the business type, you can.

Selecting None as the App type

3. Then select your app ‘Display name’ that you want and click on Create app. And keep in mind that don’t use display name as Instagram or Facebook it will give you an error. After clicking on ‘create app’ it will ask for your password to verify that you are the owner and after completing all these steps you will get an app.

4. Then, select the Instagram Graph API from the ‘Add a product and click on ‘Set up’. It will add Instagram Graph API to your app. Along with the Instagram Graph API, you will also need Facebook Login to use the Instagram Graph API, go to Dashboard and select the Facebook Login and click on ‘Set up’.

5. Then Click on settings under the Facebook login there you have to enter a ‘Valid OAuth Redirect URIs’ this is the URL that will redirect the user along with the access code after doing the verification and providing the permission needed for the app.

6. Then click on the ‘setting’ in the left panel then from the drop-down select the basic option. Inside the basic, there will be APP ID and APP secret, you have to copy these both and save them in the notepad or somewhere safe, so that we can use these credentials to access the graph API data.

7. So all the step for setting the app is done now if you want to test this app you should be the owner, a developer, or else the tester of this app. To give this permission click on ‘Roles’ in the Left Panel and select ‘roles’ now there you can add a developer for your app or you can add a test user by simply entering their Facebook username.

8. Now that you are done with adding the test user and setting the app for use, now we can begin with the coding phase but before coding, we have to link the Facebook page with the Instagram professional account.

9. Go to your Instagram account setting and make it a professional account, you can make your account a business account or a creator account.

10. After that go to Facebook and select a page if you have created it already otherwise create a new Facebook page, and then go to the page and then to page settings there you will find an Instagram option using which you can link your account with the Facebook page.

11. First we will get the access_token URL from Facebook by hovering on the tools option and then clicking on Graph API Explorer.

Then we will select the required permission if you want some extra permission you can add them here and press the ‘Generate access token’ a pop will appear just copy the URL and paste it into a notepad replace the redirect URI with your redirect URI. In this way, you can get a short-lived access token.

12. Then we will exchange this code with the long-lived access token for that you have to send a get request to the Facebook server like this in the code snippet.

13. After getting the long-lived access_token we can query the Instagram Graph API to get the user data. Like getting the post data made by the user.

14. For that we will first require the page_id and using that page_id we can query the business account associated with that Facebook page and get the id of that account.

15. After getting the Instagram_id of that account we will make a query to get all the media_id related to that account and by passing the media_id to the Facebook graph API we can get all the details regarding the post.

So in this way, you can get the long-lived access token for a user and get all the data regarding the user.

If there is any doubt or confusion you can leave a comment I will surely get back to you.

This is the GitHub link.

This is the Facebook Documentation.

Thank you for reading and if you have any suggestions you can please comment so that I can improve and provide more informative blogs to you all.

--

--

Ritik khandelwal

Software engineer, Python developer, Full Stack Web Devloper, trying learn and share