Twitter V2基础订阅无法认证及获取粉丝数据技术求助
Twitter V2基础订阅相关问题解答
一、认证问题解决(Tweepy & PyTwitter)
Tweepy V2认证
使用Client类完成OAuth 1.0a认证,确保参数准确:
import tweepy client = tweepy.Client( consumer_key="你的API密钥", consumer_secret="你的API密钥密码", access_token="你的访问令牌", access_token_secret="你的访问令牌密码" ) # 验证认证状态 try: me = client.get_me() print(f"认证成功,当前用户ID:{me.data.id}") except Exception as e: print(f"认证失败:{str(e)}")
排查要点:
- 确认Twitter开发者平台中,App已关联项目且状态为激活
- 所有密钥/令牌无拼写错误、无多余空格
- App权限设置为Read(基础订阅默认权限)
PyTwitter认证
使用最新版PyTwitter,通过OAuth参数认证:
from pytwitter import Api api = Api( consumer_key="你的API密钥", consumer_secret="你的API密钥密码", access_token="你的访问令牌", access_token_secret="你的访问令牌密码" ) # 验证认证状态 try: me = api.get_me() print(f"认证成功,当前用户名:{me.data.username}") except Exception as e: print(f"认证失败:{str(e)}")
若失败,执行pip install --upgrade pytwitter更新依赖。
二、基础订阅获取粉丝数据
Twitter V2基础订阅支持获取粉丝基础详情,调用users/:id/followers端点即可。
Tweepy实现示例
# 获取目标用户ID(示例为当前认证用户) user_id = client.get_me().data.id # 获取粉丝列表,指定需要的字段 response = client.get_users_followers( user_id, max_results=1000, user_fields=["username", "name", "created_at", "description"] ) # 输出粉丝数据 if response.data: for follower in response.data: print(f"用户名:@{follower.username},昵称:{follower.name},创建时间:{follower.created_at}") # 处理分页(粉丝超过1000条时) next_token = response.meta.get("next_token") while next_token: response = client.get_users_followers( user_id, max_results=1000, user_fields=["username", "name", "created_at", "description"], pagination_token=next_token ) for follower in response.data: print(f"用户名:@{follower.username},昵称:{follower.name},创建时间:{follower.created_at}") next_token = response.meta.get("next_token")
注意:基础订阅该端点的调用限制为每15分钟15次,需控制调用频率。
内容的提问来源于stack exchange,提问作者Krutika Lodaya
相关产品推荐
相关产品推荐

