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

如何获取Twitch API的Bearer Token 无需Twitch CLI即可程序化生成

无CLI依赖程序化获取Twitch API Bearer Token方案

前置准备

你需要提前准备好已注册Twitch应用的Client ID和Client Secret,这是获取令牌的唯一必填前置条件。

实现逻辑

调用Twitch客户端凭证授权流的令牌接口,仅需一次POST请求即可生成所需的服务端访问令牌,无需用户交互、无需安装额外工具,完美适配程序化生成需求。

请求参数说明

向https://id.twitch.tv/oauth2/token发送POST请求,携带以下表单参数:

  • client_id:你的Twitch应用Client ID
  • client_secret:你的Twitch应用Client Secret
  • grant_type:固定取值为client_credentials
  • scope(可选):如需访问特定权限接口可传入对应权限值,公开资源查询无需传入

代码示例

cURL 示例

curl -X POST 'https://id.twitch.tv/oauth2/token' \
  -d 'client_id=替换为你的Client ID' \
  -d 'client_secret=替换为你的Client Secret' \
  -d 'grant_type=client_credentials'

Python 示例

import requests

def get_twitch_token(client_id, client_secret):
    url = "https://id.twitch.tv/oauth2/token"
    payload = {
        "client_id": client_id,
        "client_secret": client_secret,
        "grant_type": "client_credentials"
    }
    resp = requests.post(url, data=payload)
    resp.raise_for_status()
    # 完整返回包含access_token、expires_in等字段,可按需提取
    return resp.json()["access_token"]

Node.js 示例

const axios = require('axios');

async function getTwitchToken(clientId, clientSecret) {
    const url = 'https://id.twitch.tv/oauth2/token';
    const params = new URLSearchParams();
    params.append('client_id', clientId);
    params.append('client_secret', clientSecret);
    params.append('grant_type', 'client_credentials');
    const resp = await axios.post(url, params);
    // 完整返回包含access_token、expires_in等字段,可按需提取
    return resp.data.access_token;
}

令牌使用说明

  1. 接口返回的access_token字段即为所需的Bearer Token,expires_in字段为令牌有效期(单位为秒,默认有效期约为60天),可本地缓存令牌减少重复请求
  2. 调用Twitch API时,需要在请求头中同时携带两个字段:
    • Authorization: Bearer <获取到的access_token>
    • Client-Id: <你的Twitch应用Client ID>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 18:24:03