3

I am starting to learn the instagram API but seem to have already hit a snag. I am trying to use the very simple examples listed on the github repository. here is the code I am executing;

from instagram.client import InstagramAPI


client_id = 'xxx'
client_secret = 'xxx'
access_token = 'xxx'
client_ip = '192.168.0.3'
api = InstagramAPI(access_token=access_token,client_id=client_id,client_secret=client_secret)



recent_media, next_ = api.user_recent_media(user_id="xxx", count=10)
for media in recent_media:
print media.caption.text

everything seems to be working and all my modules are installed, but when I run the code I get the following error in the python shell;

Traceback (most recent call last):
  File "/home/william/test2.py", line 12, in <module>
    recent_media, next_ = api.user_recent_media(user_id="xxxx", count=10)
  File "/usr/local/lib/python2.7/dist-packages/instagram/bind.py", line 197, in _call
return method.execute()
  File "/usr/local/lib/python2.7/dist-packages/instagram/bind.py", line 189, in execute
content, next = self._do_api_request(url, method, body, headers)
  File "/usr/local/lib/python2.7/dist-packages/instagram/bind.py", line 151, in _do_api_request
obj = self.root_class.object_from_dictionary(entry)
  File "/usr/local/lib/python2.7/dist-packages/instagram/models.py", line 99, in object_from_dictionary
for comment in entry['comments']['data']:
KeyError: 'data'

Does anyone have any ideas on how to fix this?

Thanks very much!

  • The error seems to indicate that whatever is being returned does not have a `data` element, causing the `KeyError`. Whenever I have encountered something like this with an API, the API was usually returning an error response. Verify that the returned data is actually what you expect. – Julio Dec 20 '15 at 00:25
  • Thanks for your response. I'm using the examples right from the [API documentation](https://github.com/Instagram/python-instagram) Why is it throwing an error then? – William Pratt Dec 20 '15 at 00:28
  • I'm not that familiar with the Instagram API. If you're using their example though, chances are it's a problem with your API key/secret/token. I'd verify that those are correct before digging further. – Julio Dec 20 '15 at 00:31

1 Answers1

1

This was answered here.

  1. Find your local copy of models.py
  2. Go to line 99, it should look like this:

    for comment in entry['comments']['data']: 
        new_media.comments.append(Comment.object_from_dictionary(comment))
    
  3. add this line right above it: if "data" in entry["comments"]:
  4. adjust the next two lines below to account for the new if statement added

    if "data" in entry["comments"]:
        for comment in entry['comments']['data']: 
           new_media.comments.append(Comment.object_from_dictionary(comment))
    

That should fix it.

Community
  • 1
  • 1