【问题标题】:How to get Twitter oauth token for nltk?如何获取 nltk 的 Twitter oauth 令牌?
【发布时间】:2022-01-11 23:31:00
【问题描述】:

我遵循https://www.nltk.org/howto/twitter.html 的所有开始指示 我的密钥文件(app_key,app_secret,oauth_token,oauth_token_secret)包含消费者密钥和访问令牌。 但是当我尝试运行这段代码时

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

我得到一个错误(下)。我认为我将访问令牌用作 oauth_token 是错误的,但我不知道在哪里可以找到需要的。

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

【问题讨论】:

    标签: python api twitter oauth nltk


    【解决方案1】:

    您将“访问令牌”用作 NLTK 的 Twitter 类的“oauth_token”是正确的。我认为您只是以错误的格式保存它。

    • app_key = Twitter API 中的 API 密钥
    • app_secret = Twitter 中的 API 密钥
    • API oauth_token = Twitter API 中的访问令牌,这是不同的
    • 来自 Twitter 中的 oath_token oauth_token_secret = Twitter API 中的访问令牌秘密

    https://www.nltk.org/howto/twitter.html 上的说明有点令人困惑,因为它要求您将 Twitter 凭据保存在单独的文件中(它假定所有凭据都存储在 credentials.txt 中)。 “credentials.txt”不是 Python 文件,因此不应引用所有标记。

    我相信您的“credentials.txt”现在如下所示。

    # 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"
    

    相反,它应该如下所示。

    # 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
    

    我收到了相同的错误消息,但此更改解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 2012-08-20
      • 1970-01-01
      • 2016-11-22
      • 2020-11-01
      • 1970-01-01
      相关资源
      最近更新 更多