大家好,我是小悟
关于抖音开放平台第三方代小程序开发的两个事件接收推送通知,是开放平台代小程序实现业务的重要功能。
授权事件推送和消息与事件推送类型都以Event的值判断。
授权事件推送通知
授权事件推送包括:推送票据、授权成功、授权解除、授权码找回。抖音服务器会向第三方平台方的授权事件接收 URL 以 POST 的方式推送相关通知。
推送票据:在第三方平台创建审核通过后,抖音服务器会向其“授权事件接收URL” 每隔10分钟以 POST 的方式推送票据。
Event的值为PUSH
{"Ticket": "8c0da4968b0d1e28acbc1d738a56607d","FromUserName": "ByteDanceMicroApp","CreateTime": "2019-01-14 12:45:10","MsgType": "Ticket","Event": "PUSH"}
授权成功:Event的值为AUTHORIZED
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "AUTHORIZED","AuthorizationCode": "授权码","AuthorizationCodeExpiresIn": 3600,"AppName": "代创建的小程序名称","AppIcon": "代创建的小程序图标","CompanyName": "代创建的小程序公司主体名称","AppSuperAdminEmail": "代创建的小程序超管掩码邮箱","AppSuperAdminMobile": "代创建的小程序超管掩码手机号"}
授权解除:Event的值为UNAUTHORIZED
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "UNAUTHORIZED"}
授权码找回:
Event的值为UPDATE_AUTHORIZED
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "UPDATE_AUTHORIZED","AuthorizationCode": "授权码","AuthorizationCodeExpiresIn": 3600}
出于安全考虑,开放平台服务端会向第三方小程序后端服务推送加密后的消息,因此,服务商需要用key解密后才能查看具体信息。另外推送消息格式均为 json,并且第三方小程序后端服务接收到后需要响应并返回字符串 success。
@PostMapping(value = "authPushMsg")public void authPushMsg(PostDataModel postData) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));StringBuffer sb = new StringBuffer();String line;while ((line = br.readLine()) != null) {sb = sb.append(line);}String postStr = sb.toString();logger.warn("授权推送消息-->获得数据为postData={}", postStr);PostDataModel postData = JSON.parseObject(postStr, PostDataModel.class);try {logger.warn("授权推送消息,开始");ServerVerification serverVerification = new ServerVerification();String msgSignature = serverVerification.getMsgSignature(thirdPlatConfig.getMsgCheckToken(), postData.getTimeStamp(), postData.getNonce(), postData.getEncrypt());if (StringUtils.equals(postData.getMsgSignature(), msgSignature)) {MsgDecrypt msgDecrypt = new MsgDecrypt(thirdPlatConfig.getMsgEncodingAesKey());String decrypt = msgDecrypt.decrypt(postData.getEncrypt());logger.warn("授权推送消息,解密结果={}", decrypt);if (decrypt.contains("Ticket") && decrypt.contains("PUSH")) {logger.warn("推送ticket协议-->开始");// 推送ticket通知ComponentVerifyTicket componentVerifyTicket = JSON.parseObject(decrypt, ComponentVerifyTicket.class);componentVerifyTicket.setComponentVerifyTicket(componentVerifyTicket.getTicket());RedisService<ComponentVerifyTicket> redisService = RedisService.load();redisService.save(componentVerifyTicket, ComponentVerifyTicket.class);logger.warn("推送ticket协议---->成功,保存到缓存中的值为:{}", JSON.toJSONString(componentVerifyTicket));} else {// 授权成功、授权解除、授权码找回AuthPushAuthorized authPushAuthorized = JSON.parseObject(decrypt, AuthPushAuthorized.class);String event = authPushAuthorized.getEvent();if (StringUtils.equals(EventEnum.AUTHORIZED.getCode(), event)) {logger.warn("推送授权成功通知-->成功,{}", JSON.toJSONString(authPushAuthorized));} else if (StringUtils.equals(EventEnum.UNAUTHORIZED.getCode(), event)) {logger.warn("推送授权解除通知-->成功,{}", JSON.toJSONString(authPushAuthorized));} else if (StringUtils.equals(EventEnum.UPDATE_AUTHORIZED.getCode(), event)) {logger.warn("推送授权码找回通知-->成功,{}", JSON.toJSONString(authPushAuthorized));}}}} catch (Exception e) {logger.error("系统异常", e);} finally {// 响应消息PrintWriter out = response.getWriter();out.print("success");}}
消息与事件推送通知
消息与事件推送包括:修改小程序名称、小程序名称重置、修改服务类目、修改小程序简介、修改小程序图标、版本审核结果、短视频挂载能力申请审核结果、抖音直播组件能力申请审核结果、获取用户手机号能力申请审核结果、分享模板审核结果、短视频/直播自主挂载能力审核结果通知、短视频/直播自主挂载能力抖音号绑定结果通知、短视频达人推广挂载能力审核结果通知、直播达人推广挂载能力审核结果通知。审核结果会向消息与事件接收 URL 进行事件推送。
修改小程序名称:
Event的值为MODIFY_APP_NAME
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "MODIFY_APP_NAME","ModifyAppNameResults": {"advice": "","reason": "小程序名称格式不符合规范","status": 0}}
小程序名称重置:Event的值为
APP_NAME_RESET_NOTIFICATION
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "APP_NAME_RESET_NOTIFICATION","AppNameResetNotification": "您好,您管理的小程序「%s」由于长时间未提交版本审核,名称已经被重置为「名称过期%s」。如需修改,请前往配置页面重新填写名称。"}
修改服务类目:Event的值为
MODIFY_APP_CATEGORY
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "MODIFY_APP_CATEGORY","ModifyAppCategoryResults": [{"category": "158,159,259","categoryName": "社交类-社交-直播","status": 1,"reason": ""}]}
修改小程序简介:
Event的值为MODIFY_APP_INTRO
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "MODIFY_APP_INTRO","ModifyAppIntroResults": {"reason": "小程序简介格式不符合规范","status": 0}}
修改小程序图标:
Event的值为MODIFY_APP_ICON
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "MODIFY_APP_ICON","ModifyAppIconResults": {"reason": "小程序名称格式不符合规范","status": 0}}
版本审核结果:Event的值为PACKAGE_AUDIT
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "PACKAGE_AUDIT","AuditResults": [{"hostName": "toutiao","reason": ["", ""],"status": 1,"auditDetail": [{"reason":"xxx","modifyGuide": {"name":"xxx","link":"xxx"},"detailFile":"xxx"}]}]}
短视频挂载能力申请审核结果:Event的值为APPLY_VIDEO_CAPABILITY
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "APPLY_VIDEO_CAPABILITY","ApplyVideoCapabilityResults": {"status": 0,"reason": "xxx"}}
抖音直播组件能力申请审核结果:Event的值为APPLY_LIVE_CAPABILITY
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "APPLY_LIVE_CAPABILITY","ApplyLiveCapabilityResults": {"status": 0,"reason": "xxx"}}
获取用户手机号能力申请审核结果:Event的值为
APPLY_PHONE_NUMBER_CAPABILITY
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "APPLY_PHONE_NUMBER_CAPABILITY","ApplyPhoneNumberCapabilityResults": {"status": 0,"reason": "xxx"}}
分享模板审核结果:
Event的值为APPLY_SHARE_TEMPLATE
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "APPLY_SHARE_TEMPLATE","ApplyShareTemplateResults": {"templateId": "xxx","status": 0,"reason": "[\"xxx\",\"xxx\"]"}}
短视频/直播自主挂载能力审核结果通知:
Event的值为APPLY_CAPACITY,需要结合
EventContent.capacity_key的值判断
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "APPLY_CAPACITY","EventContent": {// 能力key,video_self_mount-短视频自主挂载;live_self_mount-直播自主挂载。"capacity_key": "能力key","status": 1,"reason": "xxx"}}
短视频/直播自主挂载能力抖音号绑定结果通知:
Event的值为SELT_MOUNT_BIND,需要结合
EventContent.capacity_key的值判断
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "SELT_MOUNT_BIND","EventContent": {// 能力key,video_self_mount-短视频自主挂载;live_self_mount-直播自主挂载。"capacity_key": "能力key","aweme_id": "抖音号","user_name": "抖音用户昵称","bind_status": 1}}
短视频达人推广挂载能力审核结果通知:
Event的值为APPLY_CAPACITY,需要结合
EventContent.capacity_key的值判断
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "APPLY_CAPACITY","EventContent": {// 能力key,video_talent_mount-短视频达人推广挂载能力。"capacity_key": "能力key","status": 1,"reason": "xxx"}}
直播达人推广挂载能力审核结果通知:
Event的值为APPLY_CAPACITY,需要结合
EventContent.capacity_key的值判断
{"AppId": "授权小程序的appid","TpAppId": "第三方小程序应用appid","EventTime": "2019-01-14 12:45:10","Event": "APPLY_CAPACITY","EventContent": {// 能力key,ma.mount.live_talent_mountt-直播达人推广挂载能力。"capacity_key": "能力key","status": 1,"reason": "xxx"}}
出于安全考虑,开放平台服务端会向第三方小程序后端服务推送加密后的消息,因此,服务商需要用key解密后才能查看具体信息。另外推送消息格式均为 json,并且第三方小程序后端服务接收到后需要响应并返回字符串 success。
@PostMapping(value = "eventPushMsg")public void eventPushMsg(PostDataModel postData) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));StringBuffer sb = new StringBuffer();String line;while ((line = br.readLine()) != null) {sb = sb.append(line);}String postStr = sb.toString();logger.warn("事件推送消息-->获得数据为postData={}", postStr);PostDataModel postData = JSON.parseObject(postStr, PostDataModel.class);try {logger.warn("事件推送消息,开始");ServerVerification serverVerification = new ServerVerification();String msgSignature = serverVerification.getMsgSignature(thirdPlatConfig.getMsgCheckToken(), postData.getTimeStamp(), postData.getNonce(), postData.getEncrypt());if (StringUtils.equals(postData.getMsgSignature(), msgSignature)) {MsgDecrypt msgDecrypt = new MsgDecrypt(thirdPlatConfig.getMsgEncodingAesKey());String decrypt = msgDecrypt.decrypt(postData.getEncrypt());logger.warn("事件推送消息,解密结果={}", decrypt);EventPushMsg eventPushMsg = JSON.parseObject(decrypt, EventPushMsg.class);String event = eventPushMsg.getEvent();if (StringUtils.equals(EventEnum.MODIFY_APP_NAME.getCode(), event)) {logger.warn("修改名称审核结果通知-->通知,{}", JSON.toJSONString(eventPushMsg));} else if (StringUtils.equals(EventEnum.APP_NAME_RESET_NOTIFICATION.getCode(), event)) {logger.warn("名称重置通知-->通知,{}", JSON.toJSONString(eventPushMsg));} else if (StringUtils.equals(EventEnum.MODIFY_APP_INTRO.getCode(), event)) {logger.warn("修改简介审核结果通知-->通知,{}", JSON.toJSONString(eventPushMsg));} else if (StringUtils.equals(EventEnum.MODIFY_APP_ICON.getCode(), event)) {logger.warn("修改图标审核结果通知-->通知,{}", JSON.toJSONString(eventPushMsg));} else if (StringUtils.equals(EventEnum.MODIFY_APP_CATEGORY.getCode(), event)) {logger.warn("修改服务类目审核结果通知-->通知,{}", JSON.toJSONString(eventPushMsg));} else if (StringUtils.equals(EventEnum.PACKAGE_AUDIT.getCode(), event)) {logger.warn("版本审核结果通知-->通知,{}", JSON.toJSONString(eventPushMsg));} else if (StringUtils.equals(EventEnum.APPLY_VIDEO_CAPABILITY.getCode(), event)) {logger.warn("「短视频挂载」能力申请审核结果通知-->通知,{}", JSON.toJSONString(eventPushMsg));} else if (StringUtils.equals(EventEnum.APPLY_LIVE_CAPABILITY.getCode(), event)) {logger.warn("「抖音直播组件」能力申请审核结果通知-->通知,{}", JSON.toJSONString(eventPushMsg));} else if (StringUtils.equals(EventEnum.APPLY_PHONE_NUMBER_CAPABILITY.getCode(), event)) {logger.warn("「获取用户手机号」能力申请审核结果通知-->通知,{}", JSON.toJSONString(eventPushMsg));} else if (StringUtils.equals(EventEnum.APPLY_CAPACITY.getCode(), event)) {logger.warn("短视频/直播自主挂载能力审核结果通知-->通知,{}", JSON.toJSONString(eventPushMsg));logger.warn("短视频达人推广挂载能力审核结果通知---->通知,{}", JSON.toJSONString(eventPushMsg));logger.warn("直播达人推广挂载能力审核结果通知---->通知,{}", JSON.toJSONString(eventPushMsg));} else if (StringUtils.equals(EventEnum.SELT_MOUNT_BIND.getCode(), event)) {logger.warn("短视频/直播自主挂载能力抖音号绑定结果通知-->通知,{}", JSON.toJSONString(eventPushMsg));}}}} catch (Exception e) {logger.error("系统异常", e);} finally {// 响应消息PrintWriter out = response.getWriter();out.print("success");}}
您的一键三连,是我更新的最大动力,谢谢
山水有相逢,来日皆可期,谢谢阅读,我们再会
我手中的金箍棒,上能通天,下能探海