Instagram API using Python

20,876

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
Share:
20,876
Manas Chaturvedi
Author by

Manas Chaturvedi

Software Engineer 2 at Haptik

Updated on July 05, 2022

Comments

  • Manas Chaturvedi
    Manas Chaturvedi almost 2 years

    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?

  • Manas Chaturvedi
    Manas Chaturvedi over 8 years
    Perfect! Thanks for this.
  • Flavian Hautbois
    Flavian Hautbois over 8 years
    No problem. I like to use IPython to work with object-based libraries (and API libraries do that quite a lot). IPython allows for a lot of useful stuff, including automatic completion. For example you could have done my_usr. and pressed the tab key to know immediatly what the fields are. You could also use dict(my_usr) but it is not as pretty.
  • m3nda
    m3nda about 7 years
    Indeed using the interactive python autocompletion gives you a huge insight of the current objects/methdos avail everytime.