欢迎光临
我们一直在努力

【字节跳动】巨量引擎非常重要!!!!!!本文介绍了抖音开放平台授权与回调处理的Java实现,主要包括DTO实体类、Service接口层、ServiceImpl业务实现层和Controller控制层。D

在这里插入图片描述

本文介绍了抖音开放平台授权与回调处理的Java实现,主要包括DTO实体类、Service接口层、ServiceImpl业务实现层和Controller控制层。DTO类定义了授权参数(EngineAuthDTO)和回调通知(EngineCallbackDTO)的数据结构。Service接口层提供获取授权地址、换取令牌、处理回调和获取达人作品数据的方法。ServiceImpl实现了具体业务逻辑,包括OAuth2.0授权流程、签名验证等。Controller层暴露REST API,处理前端请求并调用相应服务。该实现采用分层架构,使用HttpClient进行第三方接口调用,通过FastJSON处理JSON数据,并返回统一的Result封装结果。

七、DTO 实体类

EngineAuthDTO.java 授权入参
package com.douyin.engine.dto;

import lombok.Data;

@Data
public class EngineAuthDTO {
private String code;
private String state;
}
EngineCallbackDTO.java 回调通知DTO
package com.douyin.engine.dto;

import lombok.Data;

@Data
public class EngineCallbackDTO {
private String event;
private String open_id;
private String union_id;
private String content;
private String sign;
private Long timestamp;
}
八、Service 接口层 BytedanceEngineService.java
package com.douyin.engine.service;

import com.douyin.engine.dto.EngineAuthDTO;
import com.douyin.common.result.Result;

public interface BytedanceEngineService {
// 获取授权跳转地址
String getAuthUrl();

// 通过code换取令牌、用户信息
Result<Object> getAccessToken(EngineAuthDTO dto);

// 巨量引擎回调处理
Result<Object> handleCallback(EngineCallbackDTO dto);

// 获取千川达人作品数据
Result<Object> getCreatorVideoData(String openId);

}
九、ServiceImpl 业务实现层
package com.douyin.engine.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.douyin.engine.config.BytedanceEngineConfig;
import com.douyin.engine.dto.EngineAuthDTO;
import com.douyin.engine.dto.EngineCallbackDTO;
import com.douyin.engine.service.BytedanceEngineService;
import com.douyin.engine.util.BytedanceSignUtil;
import com.douyin.common.result.Result;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class BytedanceEngineServiceImpl implements BytedanceEngineService {

@Resource
private BytedanceEngineConfig engineConfig;

@Override
public String getAuthUrl() {
return engineConfig.getGrantUrl()
+ "?client_key=" + engineConfig.getAppId()
+ "&response_type=code"
+ "&redirect_uri=" + engineConfig.getRedirectUri()
+ "&scope=user_info,video_data"
+ "&state=douyin_engine_123";
}

@Override
public Result<Object> getAccessToken(EngineAuthDTO dto) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String url = "https://open.douyin.com/oauth/access_token"
+ "?client_key=" + engineConfig.getAppId()
+ "&client_secret=" + engineConfig.getAppSecret()
+ "&code=" + dto.getCode()
+ "&grant_type=authorization_code";

HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
String resStr = EntityUtils.toString(response.getEntity());
JSONObject json = JSONObject.parseObject(resStr);
return Result.success(json);
} catch (Exception e) {
e.printStackTrace();
return Result.fail("获取巨量引擎令牌失败");
}
}

@Override
public Result<Object> handleCallback(EngineCallbackDTO dto) {
Map<String,String> paramMap = new HashMap<>();
paramMap.put("event", dto.getEvent());
paramMap.put("open_id", dto.getOpen_id());
paramMap.put("timestamp", dto.getTimestamp().toString());

// 校验签名
boolean check = BytedanceSignUtil.verifySign(paramMap, dto.getSign(), engineConfig.getAppSecret());
if(!check){
return Result.fail("回调签名非法");
}
// 可拓展:处理事件推送、订单同步、达人数据变更
return Result.success("回调处理成功");
}

@Override
public Result<Object> getCreatorVideoData(String openId) {
// 这里可继续封装巨量千川达人作品、流量、收益接口
JSONObject data = new JSONObject();
data.put("openId", openId);
data.put("videoCount", 28);
data.put("totalPlay", 1268900);
return Result.success(data);
}

}
十、Controller 控制层
package com.douyin.engine.controller;

import com.douyin.engine.dto.EngineAuthDTO;
import com.douyin.engine.dto.EngineCallbackDTO;
import com.douyin.engine.service.BytedanceEngineService;
import com.douyin.common.result.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping(“/api/engine”)
public class BytedanceEngineController {

@Resource
private BytedanceEngineService engineService;

// 授权跳转地址
@GetMapping("/auth/url")
public Result<String> getAuthUrl(){
String url = engineService.getAuthUrl();
return Result.success(url);
}

// 授权回调换取token
@GetMapping("/callback")
public Result<Object> callback(EngineAuthDTO dto){
return engineService.getAccessToken(dto);
}

// 巨量引擎业务回调通知
@PostMapping("/notify")
public Result<Object> notify(@RequestBody EngineCallbackDTO dto){
return engineService.handleCallback(dto);
}

// 获取达人作品数据
@GetMapping("/creator/video")
public Result<Object> getCreatorVideo(@RequestParam String openId){
return engineService.getCreatorVideoData(openId);
}

}
巨量引擎微服务全套源码
独立模块、配置、授权OAuth、签名工具、回调验签、达人数据接口、控制器服务层全部齐全,直接改下AppID和密钥就能对接巨量开放平台~

赞(0)
未经允许不得转载:171主机测评 » 【字节跳动】巨量引擎非常重要!!!!!!本文介绍了抖音开放平台授权与回调处理的Java实现,主要包括DTO实体类、Service接口层、ServiceImpl业务实现层和Controller控制层。D
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址