使用Tweepy时持续触发“Authentication Required”错误的排查求助
排查Tweepy持续触发“Authentication Required”错误的方案
问题描述
运行以下Python代码时持续触发「Authentication Required」错误,已尝试单双引号混用但无效,怀疑字典设置存在问题,需要排查细节:
import tweepy import time credentials = [ { "consumer_key": 'xxx', "consumer_secret": 'xxx', "access_token": 'xxx', "access_token_secret": 'xxx' }, { "consumer_key": 'xxx', "consumer_secret": 'xxx', "access_token": 'xxx', "access_token_secret": 'xxx' }, { "consumer_key": 'xxx', "consumer_secret": 'xxx', "access_token": 'xxx', "access_token_secret": 'xxx' } ] numofAccs = 3 username = "xxx" apis = [tweepy.API(tweepy.OAuthHandler(c["consumer_key"], c["consumer_secret"]).set_access_token(c["access_token"], c["access_token_secret"])) for c in credentials] latest_retweet_ids = [None] * numofAccs while True: try: # Get the latest tweet from the specified user latest_tweet = apis[0].user_timeline(screen_name=username, count=1)[0] # If this is a new tweet, retweet it from each account and update the latest_retweet_id variables if latest_tweet.id not in latest_retweet_ids: for api in apis: time.sleep(10) #Delay for 10s on purpose api.retweet(latest_tweet.id) time.sleep(10) #Delay for 10s on purpose api.create_favorite(latest_tweet.id) latest_retweet_ids = [latest_tweet.id] * numofAccs print(f"Retweeted: {latest_tweet.text} from all " + numofAccs + " accounts.") else: print("No new tweets to retweet.") except tweepy.errors.TweepyException as e: print(e) time.sleep(60) # Wait for 1 minute before checking again (Don't want to spam)
排查步骤
1. 验证凭证有效性(优先级最高)
- 每组凭证的
consumer_key、consumer_secret、access_token、access_token_secret必须是从Twitter Developer Portal生成的OAuth 1.0a凭证,确保无复制错误(如多余空格、字符遗漏)。 - 用以下最小脚本逐个验证每组凭证:
import tweepy # 替换为单组待测试凭证 test_cred = { "consumer_key": "你的consumer_key", "consumer_secret": "你的consumer_secret", "access_token": "你的access_token", "access_token_secret": "你的access_token_secret" } auth = tweepy.OAuthHandler(test_cred["consumer_key"], test_cred["consumer_secret"]) auth.set_access_token(test_cred["access_token"], test_cred["access_token_secret"]) api = tweepy.API(auth) try: user = api.verify_credentials() print(f"凭证有效,对应账号:{user.screen_name}") except tweepy.errors.TweepyException as e: print(f"凭证无效:{e}") - 若某组凭证验证失败,直接排查该组的生成流程(是否在开发者平台正确创建应用、获取授权)。
2. 检查字典键名与引用的一致性
- Python允许字典键用单/双引号混用,所以引号格式不是问题。
- 确认所有字典的键名无拼写错误:比如不要把
consumer_key写成consumer-key、ConsumerKey(大小写、符号必须完全一致)。
3. 拆分API实例创建逻辑,定位错误点
- 原代码用列表推导式链式创建API实例,可能隐藏单组凭证的错误,改成分步创建并验证:
apis = [] for idx, c in enumerate(credentials): try: auth = tweepy.OAuthHandler(c["consumer_key"], c["consumer_secret"]) auth.set_access_token(c["access_token"], c["access_token_secret"]) api = tweepy.API(auth) # 验证当前API实例是否有效 api.verify_credentials() apis.append(api) print(f"第{idx+1}组凭证创建API成功") except tweepy.errors.TweepyException as e: print(f"第{idx+1}组凭证创建API失败:{e}") - 这样能明确看到哪一组凭证导致认证错误。
4. 检查开发者账号与应用权限
- 确认关联的Twitter账号无冻结、限制情况,开发者账号状态正常。
- 检查应用权限:在Twitter Developer Portal的应用设置中,确保权限设置为读和写(默认是只读),修改权限后必须重新生成
access_token和access_token_secret(旧token会失效)。
5. 修复代码中的其他潜在问题
- 原代码的打印语句会触发类型错误:
numofAccs是整数,不能直接与字符串拼接,改成:print(f"Retweeted: {latest_tweet.text} from all {numofAccs} accounts.") - 虽然这不是认证错误的原因,但会导致代码执行到该步骤时崩溃,影响后续逻辑。
内容的提问来源于stack exchange,提问作者Mr Magoo
相关产品推荐
相关产品推荐

