【问题标题】:401 Error when retrieving Twitter data using Tweepy使用 Tweepy 检索 Twitter 数据时出现 401 错误
【发布时间】:2015-04-09 08:44:51
【问题描述】:

我正在尝试使用 Tweepy 检索 Twitter 数据,使用下面的代码,但我返回 401 错误,我重新生成访问和秘密令牌,并且出现了相同的错误。

#imports
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

#setting up the keys
consumer_key = 'xxxxxxx'
consumer_secret = 'xxxxxxxx' 
access_token = 'xxxxxxxxxx'
access_secret = 'xxxxxxxxxxxxx'

class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output

   def on_data(self, data):
       print (data)
       return True

   def on_error(self, status):
       print (status)



#printing all the tweets to the standard output
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)



stream = Stream(auth, TweetListener())

t = u"#سوريا"
stream.filter(track=[t])

【问题讨论】:

  • 您是否在您的 Twitter 帐户中授权了该应用程序? [帮助链接][1] [1]:stackoverflow.com/questions/19095257/…
  • 不幸的是,我仍然有同样的错误。
  • 我也尝试使用 twitter 字段检查登录,但没有用。任何帮助!!!
  • 我猜是你机器的日期和时间问题,请检查它们是否更新?如果您的凭据不正确或者您使用的是过时版本的 tweepy,这很常见
  • 哦,我找到了解决方案。在这个链接link,所有关于国家,时间设置在您的计算机中。谢谢所有

标签: python twitter tweepy


【解决方案1】:

只需重置系统时钟。

如果用于身份验证的 API 请求来自声称其时间超出 Twitter 时间 15 分钟的服务器,则会失败并出现 401 错误。

谢谢你

【讨论】:

  • 我认为实际上是 5 分钟,而不是 15 分钟。此外,这仅适用于流式传输请求。即使时钟不同步,REST API 请求也能正常工作。
  • 完美地解决了我的问题。由于土耳其新的全年 DST 政策,我的旧 Mac Pro 时间错误。手动修复它导致我的 tweepy 脚本给出 401 错误。谢谢。
【解决方案2】:

您可能只是在从apps.twitter.com 页面复制访问令牌时犯了一个错误。

您需要复制作为访问令牌给出的整个内容,而不仅仅是- 之后的字符串。

例如,复制并粘贴整个字符串,如74376347-jkghdui456hjkbjhgbm45gj,而不仅仅是jkghdui456hjkbjhgbm45gj

[请注意,上面的字符串只是我出于演示目的而随机输入的内容。不过,您的实际访问令牌也将如下所示,即 "一串数字-一个字母数字串"]

【讨论】:

    【解决方案3】:

    您只需在双引号中显示您的密钥 并且您不必在最后一次 twitter 身份验证中定义您的密钥。

    #Import the necessary methods from tweepy library
    from tweepy.streaming import StreamListener
    from tweepy import OAuthHandler
    from tweepy import Stream
    
    #Variables that contains the user credentials to access Twitter API
    access_token = 'X3YIzD'
    access_token_secret = 'PiwPirr'
    
    consumer_key = 'ekaOmyGn'
    consumer_secret = 'RkFXRIOf83r'
    
    
    #This is a basic listener that just prints received tweets to stdout.
    class StdOutListener(StreamListener):
    
    def on_data(self, data):
        print data
        return True
    
    def on_error(self, status):
        print status
    
    
    if __name__ == '__main__':
    
    #This handles Twitter authetification and the connection to Twitter 
    Streaming API
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)
    
    #This line filter Twitter Streams to capture data by the keywords: 'python', 
    'javascript', 'ruby'
    stream.filter(track=['python', 'javascript', 'ruby'])
    

    【讨论】:

      【解决方案4】:

      我有同样的问题 - 这里没有解决它。对我来说,诀窍是使用 Tweepy 流式传输推文显然需要 1A 身份验证,而不是 2A(请参阅 - https://github.com/tweepy/tweepy/issues/1346)。这意味着您需要在身份验证对象中使用访问令牌以及消费者令牌。

      import tweepy
      
      # user credentials
      access_token = '...'
      access_token_secret = '...'
      
      consumer_key = '...'
      consumer_secret = '...'
      
      auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
      
      # this is the main difference
      auth.set_access_token(access_token, access_token_secret)
      
      stream = tweepy.Stream(auth, tweepy.StreamListener)
      

      【讨论】:

      • 嗨,我收到未经授权的 401
      【解决方案5】:

      就我而言,发生错误是因为我使用的是AppAuthHandler 而不是OAuthHandler。切换到OAuthHandler 解决了这个问题。

      【讨论】:

        【解决方案6】:

        就我而言,我遇到了这个问题,但它与时间无关。

        我的应用拥有“只读”权限。 我必须将其更改为“读写”权限才能停止错误。

        您可以通过转到应用设置页面中的“用户身份验证”来执行此操作。

        【讨论】:

          猜你喜欢
          • 2015-01-05
          • 1970-01-01
          • 2017-02-07
          • 2014-12-29
          • 2014-12-25
          • 1970-01-01
          • 2020-09-22
          • 2012-07-19
          • 1970-01-01
          相关资源
          最近更新 更多