Search code examples
pythontwitterjupyter-notebooktwitter-oauth

Unable to access Twitter API in Jupyter Notebook. How do I solve authentication problem?


I have installed the twitter and python-twitter modules.

import tweepy as tp
import twitter

twitter_keys = {
            'consumer_key':        '*',
            'consumer_secret':     '*',
            'access_token_key':    '*',
            'access_token_secret': '*'
        }
auth=tp.OAuthHandler(twitter_keys['consumer_key'],twitter_keys['consumer_secret'])
auth.set_access_token(twitter_keys['access_token_key'],twitter_keys['access_token_key'])
api=tp.API(auth)
INDIA_WOE_ID = 23424848
results = api.trends_place(_id = 1)

And this is all it can show me.

---------------------------------------------------------------------------
TweepError                                Traceback (most recent call last)
<ipython-input-33-51682b25a788> in <module>
----> 1 results = api.trends_place(_id = 1)

~/anaconda3/lib/python3.7/site-packages/tweepy/binder.py in _call(*args, **kwargs)
    248                 return method
    249             else:
--> 250                 return method.execute()
    251         finally:
    252             method.session.close()

~/anaconda3/lib/python3.7/site-packages/tweepy/binder.py in execute(self)
    231                     raise RateLimitError(error_msg, resp)
    232                 else:
--> 233                     raise TweepError(error_msg, resp, api_code=api_error_code)
    234 
    235             # Parse the response payload

TweepError: [{'code': 32, 'message': 'Could not authenticate you.'}]

I have set up the Dev environment. I have tried regenerating my key. I have scoured through numerous websites looking for an answer, all to no avail.

Please do provide steps on how to solve said issue.

Thank you!


Solution

  • Per the Tweepy documentation, the set_access_token() command requires the access key, and the access key secret.

    Change this line:

    auth.set_access_token(twitter_keys['access_token_key'],twitter_keys['access_token_key'])

    To this:

    auth.set_access_token(twitter_keys['access_token_key'],twitter_keys['access_token_secret'])

    This should resolve your authentication error by providing the full authentication requirements.