直播平台直播API集成之快手篇

前言:
本篇我们来介绍如何使用快手 的直播API创建直播。
准备工作:
1、你首先得有个快手账号;
2、创建快手应用,填写应用审核信息,等待应用创建审核通过,应用成功创建后在开发与上线前还要提前做好API权限申请,如果你只需要获取用户基本信息,以及得到直播API的访问权限,那么只需要在应用管理页申请user_info(默认可获得)、user_base(默认可获得)、user_video_live(需手动点击申请并等待审核),审核通过后如下图所示即可进行相关API的调用:

在这里插入图片描述

3、应用管理页面你还需要填写授权回调域地址,拿快手网站应用类型举例,如果你创建的是一个网站应用,那么你需要添加官网域名,授权回调域就是一个主域名和官网域名相同的网页地址,用户授权后会重定向到你这个授权回调地址,并且在授权回调地址品上你需要的授权码,授权回调域可配置多个,因此测试环境和生产环境的授权回调地址你可以同时加上,只要你测试环境域名主域名跟生产环境一样,配置方式如下:

在这里插入图片描述

4、先调通用户授权流程获得具有直播API操作权限的token后即可调用快手直播API或者你自己对快手直播API的后台封装,授权流程我可以细说一下,这里以网站应用类型举例:
授权时帮用户拉起快手授权页:
https://open.kuaishou.com/oauth2/authorize?app_id=你的应用ID&scope=user_info,user_video_live&response_type=code&ua=pc&redirect_uri=配置在应用中的回调地址&state=自定义的随机字符串
然后用户就能看到授权页面:

在这里插入图片描述

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

private Map<String, Object> getKuaiShouToken(String code) {
        KwaiOpenOauthApi kwaiOpenOauthApi = KwaiOpenOauthApi.init(kwaiApiUtils.getClientId());
        HashMap<String, Object> result = new HashMap<>();
        try {
            AccessTokenRequest request = new AccessTokenRequest(code, kwaiApiUtils.getClientSecret());
            AccessTokenResponse response = kwaiOpenOauthApi.getAccessToken(request);
            log.info("kuaishou code:{}, response: [{}]", code, response);
            result.put("refresh_token", response.getRefreshToken());
            result.put("access_token", response.getAccessToken());
            result.put("expires_in", response.getExpiresIn());
            result.put("open_id", response.getOpenId());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

直播API封装:
代码部分使用JAVA语言实现(代码只提供主流程代码)
先附上快手直播API的官方文档。
快手的直播API功能很少,主要就是三个,创建直播地址、结束直播以及查询直播信息,连更新直播配置信息的接口都没有,可见对于快手来说,想更新直播配置要通过重新创建直播信息来实现。
获取快手直播地址的API JAVA的核心参数与封装代码:

在这里插入图片描述

public GetRtmpVo queryKuaiShouRtmpInfo(GetRtmpDto params, String userId) {
        GetRtmpVo result = new GetRtmpVo();
        log.info("[RtmpManageServiceImpl.queryKuaiShouRtmpInfo]enter service.");
        try {
            log.info("[RtmpManageServiceImpl.queryKuaiShouRtmpInfo][params:[{}]],", params);
            // 参数封装
            Date currentTime = DateUtils.getCurrentDateTime();
            KwaiOpenLiveApi session = fastHandUtils.getKwaiSession(params.getAppId());
            GetPushUrlResquest getPushUrlResquest = new GetPushUrlResquest(params.getAccessToken());
            getPushUrlResquest.setCaption(params.getCaption());
            getPushUrlResquest.setPanoramic(params.getPanoramic());
            getPushUrlResquest.setShopLive(params.isShopLive());
            getPushUrlResquest.setFile(params.getFile().getBytes());
            log.info("[RtmpManageServiceImpl.queryKuaiShouRtmpInfo][getPushUrlResquest:[{}]],", getPushUrlResquest);
            // 接口调用
            GetPushUrlResponse response = session.getPushUrl(getPushUrlResquest);
            // 返回值封装
            result.setRtmpUrl(response.getContent().getPushUrl());
            result.setLiveStreamName(response.getContent().getLiveStreamName());
        } catch (NorthException ex) {
            throw ex;
        } catch (KwaiOpenException ex) {
            log.warn("error_code:[{}], error_msg:[{}].", ex.getResult().getCode(), ex.getMessage());
            if ("100200102".equals(ex.getResult().getCode())) {
                throw new NorthException(NorthErrorEnum.UN_AUTHORIZATION);
            }
        } catch (Exception ex) {
            log.error("[RtmpManageServiceImpl.queryKuaiShouRtmpInfo]query kuaishou  rtmp failed! error msg:{}.", ex.getMessage());
            throw new NorthException(NorthErrorEnum.SERVER_INTERNAL_ERROR);
        }
        log.info("[RtmpManageServiceImpl.queryKuaiShouRtmpInfo]out service.");
        return result;
}

结束快手直播:

public HostNameVo endKuaiShouLive(EndLiveStreamDto params, String userId) {
        HostNameVo result = new HostNameVo();
        log.info("[RtmpManageServiceImpl.endKuaiShouLive]enter service.");
        try {
            KwaiOpenLiveApi session = fastHandUtils.getKwaiSession(params.getAppId() == null ? kwaiApiUtils.getClientId() : params.getAppId());
            StopPushRequest request = new StopPushRequest(params.getAccessToken(), params.getLiveStreamName());
            StopPushResponse response = session.stopPush(request);
            log.info("response: [{}]", response);
            result.setHostName(response.getHostName());
            log.info("[RtmpManageServiceImpl.endKuaiShouLive]out service.");
        } catch (NorthException ex) {
            throw ex;
        } catch (Exception ex) {
            log.error("[RtmpManageServiceImpl.endKuaiShouLive]end kuaishou  rtmp failed! error msg:{}.", ex.getMessage());
            throw new NorthException(NorthErrorEnum.SERVER_INTERNAL_ERROR);
        }
        log.info("[RtmpManageServiceImpl.endKuaiShouLive]out service.");
        return result;
    }
  • 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、付费专栏及课程。

余额充值