您的位置:首页 > 汽车 > 新车 > 直播平台直播API集成之twitch篇

直播平台直播API集成之twitch篇

2025/2/7 13:56:05 来源:https://blog.csdn.net/jspyth/article/details/141647712  浏览:    关键词:直播平台直播API集成之twitch篇

前言:
    本篇我们来介绍如何使用twitch的直播API创建直播。
准备工作:
1、你首先得有个twitch账号;
2、创建twitch应用,主要是给自己的应用取名并配置授权回调地址(可配多个),如下图所示:

在这里插入图片描述

3、先拉起用户授权页面并获得具有直播API操作权限的token后即可调用twitch直播API或者你自己对twitch直播API的后台封装,授权流程我可以细说一下,这里以网站应用类型举例:
授权时帮用户拉起twitch授权页:
https://id.twitch.tv/oauth2/authorize?response_type=code&client_id=你的应用ID&redirect_uri=你的授权回调地址&scope=channel%3Aread%3Astream_key%20channel%3Amanage%3Aschedule&state=c3ab8aa609ea11e793ae92361f002671
然后用户就能看到授权页面:

在这里插入图片描述

    用户授权之后前端即可在授权回调页面地址后得到授权码code,之后便可以调用后台封装的接口通过code得到用户的access_token去访问API了,通过code获取token也给大家给出示例:

private Map<String, Object> getTwitchToken(String code) {String url = "https://id.twitch.tv/oauth2/token?grant_type=authorization_code";url = url + "&code=" + code;url = url + "&redirect_uri=" + twitchApiUtils.getRedirectUri();url = url + "&client_id=" + twitchApiUtils.getClientId();url = url + "&client_secret=" + twitchApiUtils.getClientSecret();String resultStr = HttpUtils.post(url, "{}", null);log.info("[getTwitchToken] result str:[{}].", resultStr);JSONObject jsonObject = JSON.parseObject(resultStr);HashMap<String, Object> result = new HashMap<>();result.put("access_token", jsonObject.getString("access_token"));result.put("refresh_token", jsonObject.getString("refresh_token"));result.put("expires_in", jsonObject.getLongValue("expires_in"));result.put("token_type", jsonObject.getString("token_type"));return result;}

直播API封装:
    代码部分使用JAVA语言实现(代码只提供主流程代码),先附上twitch直播API的官方文档。
首先获取twitch直播地址:
twitch的直播推流地址实际上就是"rtmp://sfo.contribute.live-video.net/app/" + streamKey,所以获取到streamKey就相当于得到了直播推流地址了,获取streamKey的API需要传参broadcast_id参数(在twitch中等同于你的twitch账号用户ID),所以我们先获取broadcast_id:

Map<String, String> header = new HashMap<>();String authorization = "Bearer " + twitchToken;header.put("Authorization", authorization);header.put("Client-Id", twitchApiUtils.getClientId());String url = "https://api.twitch.tv/helix/users";String resultStr = HttpUtils.get(url, header);log.info("[RtmpManageServiceImpl.getTwitchLiveUrl] result str:{}.", resultStr);JSONObject object = JSON.parseObject(resultStr);String broadcastId = object.getJSONArray("data").getJSONObject(0).getString("id");// 用户ID即为broadcast_id

然后就可以调用接口获取一个streamKey:

 url = "https://api.twitch.tv/helix/streams/key?broadcaster_id=" + broadcastId;resultStr = HttpUtils.get(url, header);log.info("[RtmpManageServiceImpl.getTwitchLiveUrl] result str:{}.", resultStr);object = JSON.parseObject(resultStr);String streamKey = object.getJSONArray("data").getJSONObject(0).getString("stream_key");String rtmpUrl = "rtmp://sfo.contribute.live-video.net/app/" + streamKey;// 这就是最终的直播推流地址

至此就获取到twitch的直播推流地址了。
但是想要用户看到你的直播活动通知,还需要调用接口创建一个直播事件通知:

url = "https://api.twitch.tv/helix/schedule/segment?broadcaster_id=" + broadcastId;Map<String, Object> data = new HashMap<>();data.put("start_time", date);// 直播开始时间data.put("timezone", params.getTimezone());// 时区data.put("duration", "480");// 持续时间,单位分钟data.put("title", title);// 标题data.put("is_recurring", true);// 是否周期重复JSONObject jsonObject = new JSONObject(data);String jsonStr = jsonObject.toString();resultStr = HttpUtils.postWithHeader(url, jsonStr, header);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com