【问题标题】:python-twitter error with api.PostDirectMessageapi.PostDirectMessage 的 python-twitter 错误
【发布时间】:2020-11-10 09:29:27
【问题描述】:

我正在测试 python-twitter 的代码,一切似乎都很好,直到我尝试让它向某个 id 发布直接消息。

api.PostDirectMessage(3517181843, "some text test")

然后返回

    Traceback (most recent call last):
  File "c:/Users/Azizah Blackwood/Documents/GitHub/just-a-chatbot-test/chatbotwithdifferentlib.py", line 11, in <module>
    api.PostDirectMessage(3517181843, "some text test")
  File "C:\Users\Azizah Blackwood\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twitter\api.py", line 3044, in PostDirectMessage
    created_at=data['event']['created_timestamp'],
KeyError: 'event'

【问题讨论】:

    标签: python twitter python-twitter


    【解决方案1】:

    我的意思是返回给你的数据是一个空字典或者字典没有那个元素。

    data = {}
    print(data['event']['created_timestamp'])
    

    你会得到KeyError: 'event'

    您可以尝试传入return_json=True 并查看实际响应:

    print(api.PostDirectMessage(3517181843, "some text test", return_json=True))
    

    【讨论】:

    • 返回 [{'code': 214, 'message': "event.message_create.target.recipient_id: 'some text test' is not a valid Long"}]}
    • 好的,你去吧
    猜你喜欢
    • 2017-11-13
    • 1970-01-01
    • 2010-12-03
    • 2015-10-22
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2013-11-02
    相关资源
    最近更新 更多