我的做法与其他SO用户did
完全相同for follower in api.followers_ids('twitter'):
print api.get_user(follower).screen_name
但是收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 185, in _call
return method.execute()
File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 168, in execute
raise TweepError(error_msg, resp)
tweepy.error.TweepError: [{'message': 'Bad Authentication data', 'code': 215}]
流式传输工作只是发现所以我假设身份验证过程正常进行。
这个问题不仅限于Tweepy,例如我尝试过Twython
t = Twython(app_key=consumer_key,
app_secret=consumer_secret,
oauth_token=access_token,
oauth_token_secret=access_token_secret)
得到了非常相似的错误:
File "downloader.py", line 14, in <module>
auth = t.get_authentication_tokens()
File "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 260, in get_authentication_tokens
raise TwythonAuthError(response.content, error_code=response.status_code)
twython.exceptions.TwythonAuthError: Twitter API returned a 401 (Unauthorized), Failed to validate oauth signature and token
我已经对我的钥匙和代币进行了三次检查,确保它们没问题。我遇到的问题的根源是什么?
P.S。我认为我的问题更深刻了。 我无法获得Twython工作的示例代码。以下代码:
t = Twython(app_key=consumer_key,
app_secret=consumer_secret,
oauth_token=access_token,
oauth_token_secret=access_token_secret)
try:
search_results = t.search(q='WebsDotCom', count=50)
except TwythonError as e:
print e
for tweet in search_results['statuses']:
print 'Tweet from @%s Date: %s' % (tweet['user']['screen_name'].encode('utf-8'), tweet['created_at'])
print tweet['text'].encode('utf-8'), '\n'
生成
File "downloader.py", line 19, in <module>
search_results = t.search(q='WebsDotCom', count=50)
File "/usr/local/lib/python2.7/dist-packages/twython/endpoints.py", line 130, in search
return self.get('search/tweets', params=params)
File "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 219, in get
return self.request(endpoint, params=params, version=version)
File "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 213, 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 134, in _request
response = func(url, **requests_args)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 254, in get
return self.request('get', url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 241, in request
r.send(prefetch=prefetch)
File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 521, in send
r = self.auth(self)
File "/usr/local/lib/python2.7/dist-packages/requests_oauthlib/core.py", line 58, in __call__
if is_form_encoded or extract_params(r.body):
AttributeError: 'Request' object has no attribute 'body'
我认为它与oauth有某种关系,但更新requests_oauthlib并没有帮助。
答案 0 :(得分:2)
如果您只是将自己的帐号用于自己的帐户,请按照ib-lundren的建议操作,不要费心去玩OAuth舞蹈,只需通过Twitter开发网站指定密钥即可。
同时检查auth令牌/键/等的访问权限。你已经生成确认它提供了做你想做的必要的访问。
要确认您的访问凭据确实在Twython中有效,您可以使用:
t.verify_credentials()
如果这没有吐出有关您正在连接的帐户和最近推文的数据,那么您就不会被验证。如果是,请检查验证密钥和令牌是否具有正确的权限,具体取决于您要执行的操作。
Twitter有三种访问级别:“读取”(对公共数据的读取访问),“读取和写入”(允许发送DM,但不读取它们)和“读取,写入和直接”(允许阅读DM以及之前的访问级别。此外还有额外的(很少使用的)设置,只需一个令牌即可完全访问或更改配置文件。根据您所使用的功能,您可能需要“读取和写入”或至少在测试时“读,写,直接”。答案 1 :(得分:1)
不熟悉Tweepy,但对于Twython,您的问题是您正在尝试开始OAuth舞蹈,但已经拥有您需要的访问令牌。只需直接获取粉丝
t = Twython(app_key=consumer_key,
app_secret=consumer_secret,
oauth_token=access_token,
oauth_token_secret=access_token_secret)
t.get_followers_ids()
假设您从Twitter应用程序页面获得了访问令牌,那么访问令牌将绑定到您的用户,如果您希望为其他用户获取令牌,请按照Twython starting out上的说明详细说明OAuth舞蹈。请注意,它仅以应用凭据(无访问令牌)
开头twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens(callback_url='http://mysite.com/callback')