Question

I'm doing it exactly the same way other SO user did

for follower in api.followers_ids('twitter'):
    print api.get_user(follower).screen_name

But getting the following error:

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}]

Streaming working just find so I assume authentication process went normally.

This problem is not limited to Tweepy, for example I've tried Twython

t = Twython(app_key=consumer_key,
            app_secret=consumer_secret,
            oauth_token=access_token,
            oauth_token_secret=access_token_secret)

And got very similar error:

  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

I've triple checked my keys and tokens and sure they are allright. What is the root of the problem I'm experiencing?

p.s. I think my problem is deeper. I cannot get example code for Twython working. The following code:

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'

produces

  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'

I think it is somehow related to oauth, but updating requests_oauthlib haven't helped.

Was it helpful?

Solution 2

Unfamiliar with Tweepy but for Twython your issue is that you are trying to begin the OAuth dance but already have the access token you need. Simply fetch the followers directly

t = Twython(app_key=consumer_key,
        app_secret=consumer_secret,
        oauth_token=access_token,
        oauth_token_secret=access_token_secret)
t.get_followers_ids()

Assuming you got the access token from the Twitter Application page then the access token is bound to your user, if you wish to get a token for another user follow the instructions at Twython starting out which details the OAuth dance. Note that it starts with app credentials only (no access token)

twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens(callback_url='http://mysite.com/callback')

OTHER TIPS

If you're just using it with your own account, then follow ib-lundren's suggestion and don't bother with the OAuth dance, just specify the keys through the Twitter dev site.

Also check the access permissions for the auth tokens/keys/etc. you've generated to confirm it provides the necessary access to do what you want.

To confirm that your access credentials are actually working in Twython you can use this:

t.verify_credentials()

If that doesn't spit out data about the account you're connecting with and recent tweets then you're not verified. If it does, then check to see that the auth keys and tokens have the right permissions, depending on what you're trying to do.

Twitter has three levels of access: "read" (read access to public data), "read & write" (allows sending DMs, but not reading them) and "read, write & direct" (allows reading the DMs as well as previous levels of access. Plus there are additional (rarely used) settings to allow full access or profile updates with just a token. Depending on what functions you're after, you probably want either "read & write" or "read, write & direct" at least while testing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top