|
|
|
@ -0,0 +1,136 @@
|
|
|
|
|
package com.luoo.user.controller;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import api.Result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.aliyun.tea.TeaException;
|
|
|
|
|
import com.douyin.openapi.client.Client;
|
|
|
|
|
import com.douyin.openapi.client.models.JsGetticketRequest;
|
|
|
|
|
import com.douyin.openapi.client.models.JsGetticketResponse;
|
|
|
|
|
import com.douyin.openapi.client.models.OauthClientTokenRequest;
|
|
|
|
|
import com.douyin.openapi.client.models.OauthClientTokenResponse;
|
|
|
|
|
import com.douyin.openapi.credential.models.Config;
|
|
|
|
|
import com.github.houbb.heaven.util.lang.StringUtil;
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
import util.IdWorker;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.security.*;
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@CrossOrigin
|
|
|
|
|
@RequestMapping ("/douyin")
|
|
|
|
|
@Api(tags = "抖音签名")
|
|
|
|
|
public class DouyinController {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IdWorker idWorker;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
|
|
|
|
|
|
|
@GetMapping ("/getSignature")
|
|
|
|
|
@ApiOperation(value = "获取抖音签名", notes = "获取抖音签名")
|
|
|
|
|
public Result<Map> getDouyinSignature() {
|
|
|
|
|
|
|
|
|
|
String ticket = (String) redisTemplate.opsForValue().get("douyinTicket");
|
|
|
|
|
|
|
|
|
|
if(StringUtil.isBlank(ticket)) {
|
|
|
|
|
// 获取client_token
|
|
|
|
|
try {
|
|
|
|
|
Config config = new Config().setClientKey("aw05in5o84wsu9bn").setClientSecret("135659dbc7a68c809bd0e1e7b04e461b"); // 改成自己的app_id跟secret
|
|
|
|
|
Client client = new Client(config);
|
|
|
|
|
/* 构建请求参数,该代码示例中只给出部分参数,请用户根据需要自行构建参数值
|
|
|
|
|
token:
|
|
|
|
|
1.若用户自行维护token,将用户维护的token赋值给该参数即可
|
|
|
|
|
2.SDK包中有获取token的函数,请根据接口path在《OpenAPI SDK 总览》文档中查找获取token函数的名字
|
|
|
|
|
在使用过程中,请注意token互刷问题
|
|
|
|
|
header:
|
|
|
|
|
sdk中默认填充content-type请求头,若不需要填充除content-type之外的请求头,删除该参数即可
|
|
|
|
|
*/
|
|
|
|
|
OauthClientTokenRequest sdkRequest = new OauthClientTokenRequest();
|
|
|
|
|
sdkRequest.setClientKey("aw05in5o84wsu9bn");
|
|
|
|
|
sdkRequest.setClientSecret("135659dbc7a68c809bd0e1e7b04e461b");
|
|
|
|
|
sdkRequest.setGrantType("client_credential");
|
|
|
|
|
// Map requestHeader = new HashMap();
|
|
|
|
|
// requestHeader.put("content-type", "application/json");
|
|
|
|
|
// sdkRequest.setHeader(requestHeader);
|
|
|
|
|
OauthClientTokenResponse sdkResponse = client.OauthClientToken(sdkRequest);
|
|
|
|
|
System.out.println(sdkResponse.toString());
|
|
|
|
|
JsGetticketRequest jsGetticketRequest = new JsGetticketRequest();
|
|
|
|
|
jsGetticketRequest.setAccessToken(sdkResponse.getData().accessToken);
|
|
|
|
|
JsGetticketResponse jsGetticketResponse = client.JsGetticket(jsGetticketRequest);
|
|
|
|
|
System.out.println(jsGetticketResponse.toString());
|
|
|
|
|
redisTemplate.opsForValue().set("douyinTicket", jsGetticketResponse.data.ticket,7200, TimeUnit.SECONDS);
|
|
|
|
|
} catch (TeaException e) {
|
|
|
|
|
System.out.println(e.getMessage());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.out.println(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取ticket
|
|
|
|
|
|
|
|
|
|
// RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
// String url = "https://open.douyin.com/open/getticket/";
|
|
|
|
|
// restTemplate.get
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String nonce_str = idWorker.nextId() + "";
|
|
|
|
|
String timestamp = System.currentTimeMillis()+"";
|
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
|
map.put("nonce_str", nonce_str);
|
|
|
|
|
map.put("ticket", ticket);
|
|
|
|
|
map.put("timestamp", timestamp);
|
|
|
|
|
|
|
|
|
|
String preSignStr = "nonce_str="+idWorker.nextId()+"&ticket="+ticket+"×tamp="+System.currentTimeMillis();
|
|
|
|
|
String sign =md5FromStr(preSignStr);
|
|
|
|
|
Map responseMap = new HashMap();
|
|
|
|
|
responseMap.put("share_type", "h5");
|
|
|
|
|
responseMap.put("client_key", "aw05in5o84wsu9bn");
|
|
|
|
|
responseMap.put("nonce_str", nonce_str);
|
|
|
|
|
responseMap.put("timestamp", timestamp);
|
|
|
|
|
responseMap.put("signature", sign);
|
|
|
|
|
|
|
|
|
|
return Result.success(responseMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String md5FromStr(String inStr) {
|
|
|
|
|
MessageDigest md5;
|
|
|
|
|
try {
|
|
|
|
|
md5 = MessageDigest.getInstance("MD5");
|
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] byteArray = inStr.getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
byte[] md5Bytes = md5.digest(byteArray);
|
|
|
|
|
StringBuilder hexValue = new StringBuilder();
|
|
|
|
|
for (byte md5Byte : md5Bytes) {
|
|
|
|
|
int val = ((int) md5Byte) & 0xff;
|
|
|
|
|
if (val < 16) {
|
|
|
|
|
hexValue.append("0");
|
|
|
|
|
}
|
|
|
|
|
hexValue.append(Integer.toHexString(val));
|
|
|
|
|
}
|
|
|
|
|
return hexValue.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|