2016-06-21 26 views
1

以下のコードは、過去6ヶ月間、一部のTwitterアカウントのフォロワーを取得するために完全に機能しました。突然、今朝、コードは「Error 401:Unauthorized」を返して動作を停止しました。Twitter API用のPython Tweepyが「エラー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で問題を抱えている人々を助けるかもしれない:) –

関連する問題