TwitterAPI“令牌无效或过期”。

时间:2017-04-26 20:40:39

标签: python twitter oauth-2.0

我正在python中编写一个简单的脚本来读取Twitter列表的成员。

代码:

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呼叫是否正确?

1 个答案:

答案 0 :(得分:0)

您的代码很好,但您应该在Twitter上管理您的应用并将权限更改为“读取,写入和直接消息”。