Add an instagram api feed to your Django website pt 1 - get the data!!

Prerequisites


You need an instagram account and a facebook account. For demo purposes I am going to use my instagram and my facebook account.

Go to the facebook developers page and click create app

aaaScreenshot 2021-05-09 at 08.31.27.png



Go to basic settings of your app




facebook app basic settings


Add a platform and choose website


Add a platform


Add your website and save changes


add website


Add instagram basic display


Add instagram basic display


Create new instagram app


add instagram

create app instagram


Stay in the basic settings and add your website url to the following fields and save changes


urls

Add instagram test user, in this case my instagram account


Screenshot 2021-05-09 at 13.14.12.png

Screenshot 2021-05-09 at 13.16.09.png

Your instagram account will be pending:


Screenshot 2021-05-09 at 13.17.22.png

Go to instagram and accept the tester invite

Screenshot 2021-05-09 at 13.18.28.png


We are almost there! Keep going!

We now want to sort out access. This is done by first getting an authorisation code from instagram via your browser

Following the instructions here you must build a url like the one below using your instagram app id and your redirect_uri which in this case is my home page.

https://api.instagram.com/oauth/authorize?client_id=285542483218899&redirect_uri=https://www.chriswedgwood.com/&scope=user_profile,user_media&response_type=code

Screenshot 2021-05-09 at 13.20.58.png



Click allow

Screenshot 2021-05-09 at 13.23.09.png



You will be returned to your home page but with a code in the url

Screenshot 2021-05-09 at 13.24.46.png

Get that code although ignore the last two characters "_#" !!!

Okay now we can go to python and use this code to get a token. The code can only be used once.

client_id is the Instagram app ID and client_secret is Instagram App Secret

post_obj

python instagram

Okay so this gives us a response with a short lived token which is only valid for one hour.

We can use this to get a long lived token that is valid for 60 days that we can perpetually renew.

long token

Hallelujah!!! We have a long lived token. You must be exhausted by now. I am . What a mission!!

Now we can finally use this long lived token to query the api and actually get some data to use on our site.

success

Success ..... we have data from my instagram account and we have a token to access it that is valid for 60 days.

Now I bet you are wondering what happens when the token expires? No problem you need to refresh it like so.

renew

The basic idea is that you store the token on the server or in the db with an expiry date and before it expires that you renew it.

In my use case I only have to worry about this token but you can easily see how this approach can be extended to many users who authenticate to your app

with instagram/facebook etc.

I will build a Django app in pt 2 that can be used in any of my projects going forward that will need this functionality.

Thanks :)