【问题标题】:Tweepy: tweepy.error.tweeperror 'code' 215 'message' 'bad authentication data.'Tweepy: tweepy.error.tweeperror 'code' 215 'message' 'bad authentication data.'
【发布时间】:2019-02-10 06:11:07
【问题描述】:

我不确定这里出了什么问题。以下代码一直有效到 2 天前。我在jupyter notebook 中的python3 上使用tweepy 版本号3.6.0。现在,当我执行下面给出的代码时,我不断收到错误,TweepError: [{'code': 215, 'message': 'Bad Authentication data.'}]。我究竟做错了什么?我已经看过类似的帖子123456,但没有解决方案。请注意,我还重新生成了密钥,但错误仍然存​​在。

代码是;

import tweepy
ckey = 'xxx'
csecret = 'xxx'
atoken = 'xxx'
asecret = 'xxx'

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
# collect tweets on #MRT
for tweet in tweepy.Cursor(api.search,q="MRT",count=100,
                           lang="en",rpp=100,
                           since="2017-04-03").items():
  print (tweet.created_at, tweet.text)

执行此代码时返回以下错误,

---------------------------------------------------------------------------
TweepError                                Traceback (most recent call last)
<ipython-input-11-b3e4ffb2d94f> in <module>()
      2 for tweet in tweepy.Cursor(api.search,q="MRT",count=100,
      3                            lang="en",rpp=100,
----> 4                            since="2017-04-03").items():
      5     print (tweet.created_at, tweet.text)

~\Miniconda3\lib\site-packages\tweepy\cursor.py in __next__(self)
     47 
     48     def __next__(self):
---> 49         return self.next()
     50 
     51     def next(self):

~\Miniconda3\lib\site-packages\tweepy\cursor.py in next(self)
    195         if self.current_page is None or self.page_index == len(self.current_page) - 1:
    196             # Reached end of current page, get the next page...
--> 197             self.current_page = self.page_iterator.next()
    198             self.page_index = -1
    199         self.page_index += 1

~\Miniconda3\lib\site-packages\tweepy\cursor.py in next(self)
    106 
    107         if self.index >= len(self.results) - 1:
--> 108             data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
    109 
    110             if hasattr(self.method, '__self__'):

~\Miniconda3\lib\site-packages\tweepy\binder.py in _call(*args, **kwargs)
    248             return method
    249         else:
--> 250             return method.execute()
    251 
    252     # Set pagination mode

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

TweepError: Twitter error response: status code = 400

任何建议都会有所帮助。

【问题讨论】:

  • 您可以尝试在tweepy.Cursor() 方法中运行没有since 属性的代码吗?
  • @soumithri-chilakamarri 我按照建议删除了since 属性。现在,我收到一个新错误,TweepError: Twitter error response: status code = 400。我已经检查了这些相关的帖子12 关于这个错误,仍然没有解决方案。

标签: python-3.x jupyter-notebook tweepy


【解决方案1】:

我遇到了同样的问题。重新生成密钥和令牌解决了这个问题。

【讨论】:

    【解决方案2】:

    我用双引号解决了这个问题,我看到你用的是单引号

    CONSUMER_KEY = "xxx";

    【讨论】:

    • 单引号也对我有用:-)
    【解决方案3】:

    我复制了您的代码并在我的系统中执行了它,但我找不到任何错误。我正在使用tweepy 3.6.0Python 3.5.2。我对您的代码做了两次修改。

    import tweepy
    ACCESS_TOKEN = "#####"
    ACCESS_TOKEN_SECRET = "#####"
    CONSUMER_KEY = "####" 
    CONSUMER_SECRET = "#####"
    
    # OAuth process, using the keys and tokens
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    # Creation of the actual interface, using authentication
    api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
    # collect tweets on     #MRT
    for tweet in tweepy.Cursor(api.search,q="MRT",count=100,
                           lang="en",rpp=100).items():
        print (tweet.created_at, tweet.text)
    

    注意tweepy.API这两个参数:wait_on_rate_limitwait_on_rate_limit_notify如果您想保持推文流式传输,这两个参数很重要,因为search API 只为您提供certain amount of tweets per request

    您有一个TweepError with status code 400。根据文档,它说:

    请求无效或无法以其他方式提供服务。随附的错误消息将进一步解释。未经身份验证的请求被视为无效,并将产生此响应。

    可能的解释是您的twitter API keys 不再进行身份验证,因为您的请求超出了 twitter 的速率限制

    希望这会有所帮助。

    【讨论】:

    • 感谢您的详细回答。这很有帮助,但错误仍然存​​在。可能是因为这个应用程序最初是在https://apps.twitter.com/ 上创建的。直到 2 天前,我还在使用这个网站。然后我收到一个通知,说该网站将被停用,并转移到 https://developer.twitter.com/。所以我申请了开发者账号。我的旧应用程序现在在新网站上。从那以后在执行代码时,我不断收到TweepError: Twitter error response: status code = 400。任何建议都会很有帮助。
    • 开发者帐户需要多长时间才能在这个新网站https://developer.twitter.com/ 上获得批准?有什么想法吗?
    • 因此,usersite 流式传输 API's 似乎被替换为 Account Activity API。要使用此 API,您应该拥有高级帐户或企业帐户。更多细节在这里:developer.twitter.com/en/docs/accounts-and-users/…
    • 您能否尝试使用此链接中的curl 方法检查您是否收到回复 --> developer.twitter.com/en/docs/tweets/search/api-reference/…
    • 对于任何阅读这篇文章的人,请注意它与网站迁移到developer.twitter.com无关。 @soumithri-chilakamarri 我发现了错误。错误是我将密钥放在一个文本文件中,并用单引号将它们括起来。然后我在我的程序中阅读这个文本文件。我删除了包裹在键周围的单引号,读取了程序中的文本文件和中提琴,程序正常工作。非常感谢。我已将您的回复标记为答复。
    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多