【问题标题】:Why can I not get media calls from the instagram API?为什么我无法从 instagram API 获得媒体调用?
【发布时间】:2015-12-19 19:22:26
【问题描述】:

我开始学习 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'

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

非常感谢!

【问题讨论】:

  • 该错误似乎表明返回的任何内容都没有data 元素,从而导致KeyError。每当我在 API 中遇到类似情况时,API 通常都会返回错误响应。验证返回的数据是否确实是您所期望的。
  • 感谢您的回复。我正在使用API documentation 中的示例,那为什么会抛出错误?
  • 我对 Instagram API 不是很熟悉。但是,如果您使用的是他们的示例,则很可能是您的 API 密钥/秘密/令牌有问题。在进一步挖掘之前,我会验证这些是否正确。

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


【解决方案1】:

已回答here

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

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

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

这应该可以解决它。

【讨论】:

    猜你喜欢
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2021-12-02
    • 2021-07-19
    相关资源
    最近更新 更多