用python回复twitter直接消息

时间:2016-05-03 00:01:50

标签: python twitter

我正在尝试编写一个程序来回复发送特定直接消息的用户。我是python的新手,所以非常感谢任何帮助或建议。谢谢你们,这是我的代码:

from twitter import *
import time

#Sets up twitter library
from twitter import * 
access_token = 'replaced'
access_token_secret = 'with'
consumer_key = 'this'
consumer_secret = 'stuff'

twitter_api = Twitter(auth=OAuth(access_token, access_token_secret, consumer_key, consumer_secret))
twitter_userstream = TwitterStream( domain='userstream.twitter.com')
for msg in twitter_userstream.user():
    if 'weather' in msg:
        twitter_api.direct_messages.new(user=user, text='here is your current weather update.')
    else:
        twitter_api.direct_messages.new(user=user, text="That is not an acceptable request, try entering 'weather.'")

这是它出现的401错误:

for msg in twitter_userstream.user():
  File "build/bdist.linux-armv7l/egg/twitter/api.py", line 312, in __call__
  File "build/bdist.linux-armv7l/egg/twitter/stream.py", line 284, in _handle_response
  File "build/bdist.linux-armv7l/egg/twitter/stream.py", line 209, in handle_stream_response
twitter.api.TwitterHTTPError: Twitter sent status 401 for URL: 1.1/user.json using parameters: (None)
details: <html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\n<title>Error 401 Unauthorized</title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing '/1.1/user.json'. Reason:
<pre>    Unauthorized</pre>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

请在此处查看Twython图书馆https://github.com/ryanmcgrath/twython

在这里:https://twython.readthedocs.io/en/latest/index.html

寻求可能的解决方案。