为什么我不能从Instagram API获取媒体调用?

时间:2015-12-19 19:22:26

标签: python python-2.7 api instagram instagram-api

我开始学习Instagram API,但似乎已经遇到了麻烦。我正在尝试使用github存储库中列出的非常简单的示例。这是我正在执行的代码;

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

一切似乎都在运行,我的所有模块都已安装,但是当我运行代码时,我在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'

有没有人对如何解决这个问题有任何想法?

非常感谢!

1 个答案:

答案 0 :(得分:1)

回答here

  1. 找到 models.py
  2. 的本地副本
  3. 转到第99行,它应该如下所示:

    for comment in entry['comments']['data']: 
        new_media.comments.append(Comment.object_from_dictionary(comment))
    
  4. 在其正上方添加此行:if "data" in entry["comments"]:
  5. 调整下面的两行,以考虑添加的新if语句

    if "data" in entry["comments"]:
        for comment in entry['comments']['data']: 
           new_media.comments.append(Comment.object_from_dictionary(comment))
    
  6. 那应该解决它。