直播平台直播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);
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jspyth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值