Search code examples
pythondjangoinstagram-apidjango-cms

How can i show a simple instagram feed with the new api permissions


I am trying to set up a instagram plugin on django cms to show my recent images on a homepage by the username set in the plugin. It seems you can no longer simply get public content from instagram. This is the current method im using with my sandbox account.

user_request = requests.get("https://api.instagram.com/v1/users/search?q=" + str(instance.username) + "&access_token=" + settings.INSTAGRAM_ACCESS_TOKEN)
user_id = user_request.json().values()[1][0]["id"]
r = requests.get("https://api.instagram.com/v1/users/"+ user_id +"/media/recent/?access_token=" + settings.INSTAGRAM_ACCESS_TOKEN + "&count=" + str(instance.limit))
recent_media = r.json()

The code above gets the username from the plugin model and makes a get request to the instagram api to get that user (i've heard this method doesn't always show the correct user as its just searching and getting the first user from a list).

Is this the right way to work with the instagram api and python, I was going to use python-instagram but that is no longer supported. The only other way i can think of doing it is authenticating the user on the site and using their access token which seems silly for what i need it for.

EDIT: Would removing the username field and adding access_token instead be a better method ? Then use "/users/self/media/recent" and rule out the query to search for a user by username.


Solution

  • Yes, the first results is not always the match, you have loop through all the search results and compare the username to your searched username, and then get id

    Something like this:

    var user_id = 0;
    user_request.json().values()["data"].forEach(function(user){
        if(user.username == str(instance.username)){
            user_id = user["id"]
        }
    });
    if(user_id){
        // make api call
    } else {
        // user not found
    }