2016-06-21 165 views
1

以下代码在过去6个月内完美地检索了一些Twitter帐户的关注者。突然今天早上,代码已停止工作返回'错误401:未经授权'。Twitter Tweepy for Twitter API返回'错误401:未经授权'

我在dev.twitter.com上检查了我的应用程序,它仍然有效。我更新了Tweepy。不知道为什么这不工作了。 'Cursor.next'行上的代码不起作用。

import tweepy 
import mysql.connector 
import time 

consumer_key = 'secretkey' 
consumer_secret = 'secretsecret' 
access_token = 'secrettoken' 
access_token_secret = 'secrettokensecret' 
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 



for twit_name in twit_name_array: 
api = tweepy.API(auth) 
t0= time.clock() 

data = api.rate_limit_status() 
user_followers_remaining = data['resources']['followers']['/followers/ids']['remaining'] 
print(user_followers_remaining) 
id_i = twit_name[1] 

countpage = 0 
countx = 0 

def limit_handled(cursor): 
    while True: 
     data = api.rate_limit_status() 
     user_followers_remaining = data['resources']['followers']['/followers/ids']['remaining'] 

     if user_followers_remaining>0: 
      try: 
       yield cursor.next() 
      except BaseException as e: 
       print('failed_on_CURSOR_NEXT', str(e)) 
       global api 
       api = tweepy.API(auth) 
       try: 
        yield cursor.next() 
       except BaseException as e: 
        print('failed_on_CURSOR_NEXT_2', str(e)) 
        break 
     else: 
      for min_remain in range(-3, 0): 
       print('##### TIMEOUT ##### ----- Out of queries, waiting ' + str(min_remain*5) + 'min') 
       time.sleep(5*60) 
+0

也许你需要再次OAuth登录,刷新它? – advance512

+0

我试过了。我也尝试了一些其他的查询(tweet_get)或(时间轴),他们都没有工作了... – ylnor

+0

有没有在这个问题上的更新?我一直在试图解决类似的问题,现在 –

回答

0

正如@ advance512所述,我不得不再次登录才能解决此问题。下面这段代码做了诀窍:

auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 
api = tweepy.API(auth) 

def limit_handled(cursor): 
    while True: 
     try: 
      yield cursor.next() 
     except BaseException as e: 
      print('failed_on_CURSOR_NEXT', str(e)) 
      time.sleep(5) 
      global api 
      api = tweepy.API(auth) 
      yield cursor.next() 

for followers in limit_handled(tweepy.Cursor(api.followers_ids, id = id_i).pages()): 
    for fll in followers: 
     process_follower(fll) 
+0

不幸的是,这不是我的解决方案。但是,谢谢你提供了一个答案,它可能会帮助人们遇到twitter API的问题:) –

相关问题