2017-04-26 47 views
0

我正在Python中編寫一個簡單的腳本來讀取Twitter列表的成員。TwitterAPI「標記無效或過期。」

代碼:

TW_CONSUMER_KEY = '<consumer key>' 
TW_CONSUMER_SECRET = '<consumer secret>' 
TW_ACCESS_TOKEN = '<access token>' 
TW_ACCESS_TOKEN_SECRET = '<token secret>' 

twapi = TwitterAPI(TW_CONSUMER_KEY, TW_CONSUMER_SECRET, TW_ACCESS_TOKEN, TW_ACCESS_TOKEN_SECRET) 

def list_members(slug, owner_screen_name, limit=1e10): 
    """See https://dev.twitter.com/rest/reference/get/lists/members""" 
    cursor = -1 
    members = [] 
    try: 
     response = twapi.request('lists/members', {'slug': slug, 'owner_screen_name': owner_screen_name, 'cursor': cursor, 'count': 5000}) 
     if response.status_code in [88, 130, 420, 429]: # rate limit 
      sys.stderr.write('Error for %s/%s: %s\nSleeping for 5 minutes...\n' % (owner_screen_name, slug, response.text)) 
      time.sleep(301) 
     elif response.status_code != 200: 
      sys.stderr.write('Skipping bad query: %s\n' % response.text) 
      return members 
     else: 
      items = [r['screen_name'] for r in response if 'screen_name' in r] 
      return items 
    except Exception as e: 
     sys.stderr.write('Error: %s\nskipping...\n' % e) 
     return members 
    return members 

此代碼引發錯誤:

Skipping bad query: {"errors":[{"code":89,"message":"Invalid or expired token."}]} 

我已經更新了令牌和消費者密鑰,即使文件說,他們不會過期,但徒勞無功。

可以做些什麼關於這個問題? TwitterAPI調用是否正確?

+0

您是否使用twitter登錄以獲取訪問令牌和訪問令牌的密碼?您是使用訪問令牌的響應/ oauth/access_token,而不是請求中的訪問令牌? https://dev.twitter.com/web/sign-in/implementing –

+0

我用我的令牌的確切代碼,它工作正常。嘗試重新生成您的訪問令牌。 – Jonas

回答

0

您的代碼沒問題,但您應該在Twitter上管理您的應用程序並將權限更改爲「讀取,寫入和直接發送」。