How to get Twitter oauth token for nltk?

Viewed 83

I was following all begining instructions from https://www.nltk.org/howto/twitter.html My file with keys(app_key, app_secret, oauth_token,oauth_token_secret) contains consumer keys and access tokens. But when i try to run this code

from nltk.twitter import Twitter
tw = Twitter()
tw.tweets(keywords='love, hate', limit=10) #sample from the public stream

i get an error(below). I think i am wrong at using acess tokens as oauth_token but i don`t know where i can find a needed.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_3748/1332530833.py in <module>
      1 from nltk.twitter import Twitter
      2 tw = Twitter()
----> 3 tw.tweets(keywords='love, hate', limit=10) #sample from the public stream
 
~\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\twitter\twitterclient.py in tweets(self, keywords, follow, to_screen, stream, limit, date_limit, lang, repeat, gzip_compress)
    400                 self.streamer.sample()
    401             else:
--> 402                 self.streamer.filter(track=keywords, follow=follow, lang=lang)
    403         else:
    404             self.query.register(handler)
 
~\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\twitter\twitterclient.py in filter(self, track, follow, lang)
    114                     msg = "Please supply a value for 'track', 'follow'"
    115                     raise ValueError(msg)
--> 116                 self.statuses.filter(track=track, follow=follow, lang=lang)
    117             except requests.exceptions.ChunkedEncodingError as e:
    118                 if e is not None:
 
~\AppData\Local\Programs\Python\Python310\lib\site-packages\twython\streaming\types.py in filter(self, **params)
     45         url = 'https://stream.twitter.com/%s/statuses/filter.json' \
     46               % self.streamer.api_version
---> 47         self.streamer._request(url, 'POST', params=params)
     48 
     49     def sample(self, **params):
 
~\AppData\Local\Programs\Python\Python310\lib\site-packages\twython\streaming\api.py in _request(self, url, method, params)
    135 
    136         while self.connected:
--> 137             response = _send(retry_counter)
    138 
    139             for line in response.iter_lines(self.chunk_size):
 
~\AppData\Local\Programs\Python\Python310\lib\site-packages\twython\streaming\api.py in _send(retry_counter)
    124                 else:
    125                     if response.status_code != 200:
--> 126                         self.on_error(response.status_code, response.content, response.headers)
    127 
    128                     if self.retry_count and \
 
TypeError: Streamer.on_error() takes 3 positional arguments but 4 were given
1 Answers

You are correct in using "Access Token" as "oauth_token" for the Twitter class of NLTK. I think you just saved it in a wrong format.

  • app_key = API Key in Twitter API
  • app_secret = API Secret in Twitter
  • API oauth_token = Access Token in Twitter API, which is different
  • from oath_token in Twitter oauth_token_secret = Access Token Secret in Twitter API

The instructions on https://www.nltk.org/howto/twitter.html is somewhat confusing since it asks you to save the Twitter credentials in a separate file (it assumes that all credentials are stored in credentials.txt). "credentials.txt" is not a Python file, so all tokens should NOT be quoted.

I believe your "credentials.txt" looks like below now.

# credentials.txt (these tokens are all examples and do not work)
app_key="ay1qYmfxIIOBriDwFq+HGQaa"
app_secret="zfBgkzGw+zUestZTQ7bQHQet"
oauth_token="pz0Tb/czbxlFXyZulDWlpAxx"
oauth_token_secret="UYj7hbfpxQsr5KHcu77eUQ43"

Instead, it should look like the one below.

# credentials.txt (these tokens are all examples and do not work)
app_key=ay1qYmfxIIOBriDwFq+HGQaa
app_secret=zfBgkzGw+zUestZTQ7bQHQet
oauth_token=pz0Tb/czbxlFXyZulDWlpAxx
oauth_token_secret=UYj7hbfpxQsr5KHcu77eUQ43

I had the same error message, but this change resolved my issue.

Related