Search code examples
httpwebbackendinstagram-api

Get an access token from Instagram API WITHOUT a browser?


I'm working on a project where I pull in data from social media according to certain queries and analyze them. Currently I'm pulling in Instagram media data. The only thing I want to do is to input a query and get JSON media/likes/comments.... I read the documentation of their API. I found that I couldn't get an access token without authorizing using my own account in a browser. I know there's gotta be a user, so I assume using my own account is fine. But the problem is that I don't want to get an access token manually from a browser.

Currently I'm hardcoding the access token. It will expire and I have to update it manually every time. I bet there's a better solution. Could someone help me out? Thanks.

class instagramFeed():

    CLIENT_ID = "MY-APP-ID" 
    REDIRECT_URI = "MY-ORGANIZATIONS-URI"
    ACCESS_TOKEN = "I MANUALLY GENERATE THIS TOKEN"
    query = None

    def __init__(self):
        None

    def get_data(self, query):
        response = urllib2.urlopen("https://api.instagram.com/v1/tags/search?q="+query+"&access_token="+self.ACCESS_TOKEN)

Solution

  • It is not possible to get access_token without logging in on Instagram page, thats the whole point of oauth authentication.

    You will have to manually get access_token and update, from my experience the Instagram access_token never expires, I have used it for 6 years. Only time you have to get new access_token is if Instagram revokes it because of suspicious activity detected. So as long as you use the API carefully without automating too much back-to back calls, you should be good with the same access_token.