Instagram API using Python
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
Comments
-
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 over 8 yearsPerfect! Thanks for this.
-
Flavian Hautbois over 8 yearsNo 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 usedict(my_usr)
but it is not as pretty. -
m3nda about 7 yearsIndeed using the interactive python autocompletion gives you a huge insight of the current objects/methdos avail everytime.