You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python从Twitch API获取authentication token与refresh token

Twitch API token获取失败修复方案
  • 重定向URI参数不匹配是该问题的核心诱因:你当前使用的https://localhost缺少端口号,pytwitchapi v2.2.2默认本地回调服务监听端口为17563,需先到Twitch开发者控制台将应用的重定向URI修改为http://localhost:17563(本地开发推荐用HTTP跳过证书校验),代码中初始化认证类时传入的redirect_uri参数必须和控制台配置完全一致,不能存在路径、结尾斜杠、大小写差异。
  • 端口冲突处理:如果17563端口被占用,可在初始化UserAuthenticator时通过port参数指定其他可用端口,同时同步修改Twitch控制台的重定向URI端口。
  • 手动兜底获取方案:如果点击授权后浏览器提示站点不可访问,无需等待页面加载,直接复制浏览器地址栏的完整URL,提取URL中code参数的value值,调用认证实例的generate_user_token(授权码值)方法即可手动生成authentication token和refresh token。

可用参考代码

from twitchAPI.twitch import Twitch
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.type import AuthScope

# 替换为自身应用的app_id和app_secret
twitch = Twitch('your_app_id_here', 'your_app_secret_here')
# 按实际需求调整申请的权限范围
required_scopes = [AuthScope.USER_READ_EMAIL, AuthScope.CHANNEL_MANAGE_BROADCAST]

# 重定向URI必须和Twitch控制台配置完全一致
authenticator = UserAuthenticator(
    twitch, 
    required_scopes, 
    redirect_uri='http://localhost:17563'
)
access_token, refresh_token = authenticator.authenticate()

print(f"Access Token: {access_token}")
print(f"Refresh Token: {refresh_token}")

内容的提问来源于stack exchange,提问作者Androtex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 07:06:02