我的做法与其他 SO 用户的做法完全相同
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
我已经三次检查了我的钥匙和令牌,并确保它们没问题。我遇到的问题的根源是什么?
ps 我认为我的问题更深。我无法获得 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 并没有帮助。