Search code examples
pythonpython-2.7apiinstagraminstagram-api

Instagram API using Python


I am currently using Instagram API in the sandbox mode using Python's python-instagram library. Now, I have an application and its associated client id, client secret and access token, and one connected sandbox user.

Earlier today, I was experimenting with the users/search endpoint. First, I directly used their endpoint URL to send a request:

https://api.instagram.com/v1/users/search?q=XXXX&access_token=<my_access_token> 

where XXXX is the connected sandbox user of my Instagram application. This is the generated response:

{"meta":{"code":200},"data":[{"username":"XXXX","bio":"Twitter: @XXXX","website":"","profile_picture":"https:a.jpg","full_name":"XXXX XXXX","id":"22222222"}]}

Now, I tried using the python-instagram library to send request to the same endpoint as follows:

from instagram.client import InstagramAPI
access_token = <my_access_token>

api = InstagramAPI(client_secret='aaaa', access_token = access_token[0])
usr = api.user_search('XXXX')

print usr

However, this is the response I get in this case:

[User: XXXX]

Why is it that I get different responses when I try to call the same endpoint using the direct URL and the Python library?


Solution

  • What python-instagram is doing is that it will take the raw JSON response you get when you issue an HTTP request, and map it to python objects.

    When you issue a print usr, you are printing a User object that's in a list, so you see a string which is [User: XXXX].

    You can find the model they use for the User object here. It actually directly maps the fields from the Json to get attributes.

    Try the following code to retrieve a username and id:

    my_usr = usr[0]
    print 'User id is', my_usr.id, 'and name is ', my_usr.username