【发布时间】:2014-06-08 11:52:26
【问题描述】:
我正在使用以下代码,它有一段时间像魅力一样工作,但最近在 10 次尝试中有 9 次我从 twitter api 收到错误
from twython import Twython, TwythonError
from pymongo import Connection
import pika, time
connection = Connection("host")
connection.admin.authenticate('admin', 'passwords')
db = connection['tweets']
consumer_key="key"
consumer_secret="secret"
access_token="token"
access_token_secret="secret_token"
t = Twython(app_key=consumer_key,
app_secret=consumer_secret,
oauth_token=access_token,
oauth_token_secret=access_token_secret
)
Q ='download_friends'
r_connection = pika.BlockingConnection(pika.ConnectionParameters(
'host'))
channel = r_connection.channel()
channel.queue_declare(queue= Q,
arguments={'x-message-ttl':600000})
while 1:
method_frame, header_frame, id = channel.basic_get(Q)
if db.friends.find_one({"_id": id}) != None:
print "Panic, user already exists"
continue
try:
r = t.get_friends_list(user_id = id)
except Exception as ex:
print ex, id
else:
print id
r['_id'] = id
r['time'] = time.time()
db.friends.save(r)
time.sleep(121)
Twitter API 返回 401(未授权),发生错误 处理您的请求。
这是一个堆栈跟踪
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/twython/endpoints.py", line 290, in get_friends_list
return self.get('friends/list', params=params)
File "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 230, in get
return self.request(endpoint, params=params, version=version)
File "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 224, in request
content = self._request(url, method=method, params=params, api_call=url)
File "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 194, in _request
retry_after=response.headers.get('retry-after'))
twython.exceptions.TwythonAuthError: Twitter API returned a 401 (Unauthorized), An error occurred processing your request.
在剩下的 1 次尝试中,我实际上是在获取用户的朋友。
我在机器上搜索了一下并更正了时间(因为他们说这是导致此错误的最常见原因之一),但错误仍然存在。
还有一些不工作的例子:
twitter = Twython('API key', 'API secret', oauth_version=2)
ACCESS_TOKEN = twitter.obtain_access_token()
print ACCESS_TOKEN
t = Twython('API key', access_token=ACCESS_TOKEN)
r = t.get_friends_list(user_id = 'id')
它能够获得 ACCESS_TOKEN 但仅此而已。
【问题讨论】:
-
@AlexThornton 不,这不是重复的。我的代码有时有效,有时返回和错误,而在链接上的问题是关于如何制作工作代码。在我的情况下,一些隐藏变量开始发挥作用,否则好的代码返回和错误,我无法找到它。
-
那么错误发生在哪里?你能发布一个完整的回溯吗?
-
@dorvak 这是 twitter api 返回错误。有时。有时不会,但最近错误率越来越高。
-
@Moonwalker 您是否收到带有 Twitter 错误代码的 JSON 错误响应以及您的 401 HTTP 状态返回?可以分享一下吗?
标签: python twitter oauth twitter-oauth