|
@@ -0,0 +1,245 @@
|
|
|
+package com.sqx.modules.utils;
|
|
|
+
|
|
|
+
|
|
|
+import com.aliyun.tea.TeaException;
|
|
|
+import com.douyin.openapi.client.Client;
|
|
|
+import com.douyin.openapi.client.models.*;
|
|
|
+import com.douyin.openapi.credential.models.Config;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class DouyinUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取抖音token
|
|
|
+ * token 2小时有效 使用定时任务每2小时获取一次
|
|
|
+ * @param appId
|
|
|
+ * @param appSecret
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getDouYinToken(String appId, String appSecret) {
|
|
|
+ try {
|
|
|
+ Config config = new Config().setClientKey(appId).setClientSecret(appSecret); // 改成自己的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(appId);
|
|
|
+ sdkRequest.setClientSecret(appSecret);
|
|
|
+ sdkRequest.setGrantType("client_credential");
|
|
|
+ OauthClientTokenResponse sdkResponse = client.OauthClientToken(sdkRequest);
|
|
|
+ String accessToken = sdkResponse.getData().getAccessToken();
|
|
|
+ return accessToken;
|
|
|
+ } catch (TeaException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception{
|
|
|
+ String url = "https://open.douyin.com/api/apps/trade/v2/fulfillment/delivery_prepare";
|
|
|
+
|
|
|
+ // 替换为你实际获取到的 client_token(无需用户授权)
|
|
|
+ String accessToken = "clt.xxxxxxxx";
|
|
|
+
|
|
|
+ // 构造请求体参数(明文密文二选一)
|
|
|
+ Map<String, Object> requestBody = new HashMap<>();
|
|
|
+ requestBody.put("code", "your_order_id_here"); // 明文
|
|
|
+ requestBody.put("encrypted_data", "your_code_here"); // 密文
|
|
|
+
|
|
|
+ // 创建 HttpClient
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+
|
|
|
+ // 设置请求头
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+ httpPost.setHeader("access-token", accessToken);
|
|
|
+
|
|
|
+ // 请求体
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ String jsonBody = objectMapper.writeValueAsString(requestBody);
|
|
|
+ httpPost.setEntity(new StringEntity(jsonBody, "UTF-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = client.execute(httpPost);
|
|
|
+ String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+
|
|
|
+ // 输出结果
|
|
|
+ System.out.println("响应内容:");
|
|
|
+ System.out.println(responseString);
|
|
|
+
|
|
|
+ client.close();
|
|
|
+ /*正常示例:{
|
|
|
+ "data": {
|
|
|
+ "error_code": 0,
|
|
|
+ "description": "success",
|
|
|
+ "verify_token": "xxx",
|
|
|
+ "out_order_no": "out123456",
|
|
|
+ "order_id": "ot123456",
|
|
|
+ "certificates": [
|
|
|
+ {
|
|
|
+ "encrypted_code": "xxx",
|
|
|
+ "certificate_id": "123456",
|
|
|
+ "item_order_id": "ot123"
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ "extra": {
|
|
|
+ "sub_error_code": 0,
|
|
|
+ "sub_description": "success",
|
|
|
+ "logid": "2022092115392201020812109511046",
|
|
|
+ "now": 1663745962686,
|
|
|
+ "error_code": 0,
|
|
|
+ "description": "success"
|
|
|
+ }
|
|
|
+}*//*异常示例:{
|
|
|
+ "data": {
|
|
|
+ "error_code": 2190004,
|
|
|
+ "description": "应用未获得该能力, 请去https://open.douyin.com/申请"
|
|
|
+ },
|
|
|
+ "extra": {
|
|
|
+ "sub_error_code": 0,
|
|
|
+ "sub_description": "",
|
|
|
+ "logid": "2022092115392201020812109511046",
|
|
|
+ "now": 1663745962686,
|
|
|
+ "error_code": 2190004,
|
|
|
+ "description": "应用未获得该能力, 请去https://open.douyin.com/申请"
|
|
|
+ }
|
|
|
+}*/
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 核验(明文密文二选一)
|
|
|
+ * @param token
|
|
|
+ * @param code 明文
|
|
|
+ * @param encryptedData
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static void verify(String token,String code,String encryptedData) throws Exception{
|
|
|
+ String url = "https://open.douyin.com/api/apps/trade/v2/fulfillment/delivery_prepare";
|
|
|
+
|
|
|
+ // 替换为你实际获取到的 client_token(无需用户授权)
|
|
|
+ String accessToken = token;
|
|
|
+
|
|
|
+ // 构造请求体参数(明文密文二选一)
|
|
|
+ Map<String, Object> requestBody = new HashMap<>();
|
|
|
+ requestBody.put("code", code); // 明文
|
|
|
+ requestBody.put("encrypted_data", encryptedData); // 密文
|
|
|
+
|
|
|
+ // 创建 HttpClient
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+
|
|
|
+ // 设置请求头
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+ httpPost.setHeader("access-token", accessToken);
|
|
|
+
|
|
|
+ // 请求体
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ String jsonBody = objectMapper.writeValueAsString(requestBody);
|
|
|
+ httpPost.setEntity(new StringEntity(jsonBody, "UTF-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = client.execute(httpPost);
|
|
|
+ String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+
|
|
|
+ // 输出结果
|
|
|
+ System.out.println("响应内容:");
|
|
|
+ System.out.println(responseString);
|
|
|
+
|
|
|
+ client.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 撤销核验
|
|
|
+ * @param token
|
|
|
+ * @param certificateId 代表一张券码的标识(验券时返回)
|
|
|
+ * @param orderId 需要撤销的certificate_id所属的订单id(用户提供)
|
|
|
+ * @param verifyId 代表券码一次核销的唯一标识(验券时返回)
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static void verifyCancel(String token, String certificateId,String orderId,String verifyId) throws Exception{
|
|
|
+ String url = "https://open.douyin.com/api/trade/v2/fulfillment/verify_cancel/";
|
|
|
+
|
|
|
+ // 替换为你实际获取到的 client_token(无需用户授权)
|
|
|
+ String accessToken = token;
|
|
|
+
|
|
|
+ // 构造请求体参数(明文密文二选一)
|
|
|
+ Map<String, Object> requestBody = new HashMap<>();
|
|
|
+ requestBody.put("certificate_id", certificateId);
|
|
|
+ requestBody.put("order_id", orderId);
|
|
|
+ requestBody.put("verify_id",verifyId);
|
|
|
+
|
|
|
+ // 创建 HttpClient
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+
|
|
|
+ // 设置请求头
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+ httpPost.setHeader("access-token", accessToken);
|
|
|
+
|
|
|
+ // 请求体
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ String jsonBody = objectMapper.writeValueAsString(requestBody);
|
|
|
+ httpPost.setEntity(new StringEntity(jsonBody, "UTF-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = client.execute(httpPost);
|
|
|
+ String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+
|
|
|
+ // 输出结果
|
|
|
+ System.out.println("响应内容:");
|
|
|
+ System.out.println(responseString);
|
|
|
+
|
|
|
+ client.close();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*public static void main(String[] args) {
|
|
|
+ try {
|
|
|
+ Config config = new Config().setClientKey("tt******").setClientSecret("cbs***"); // 改成自己的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之外的请求头,删除该参数即可
|
|
|
+ *//*
|
|
|
+ CouponBatchConsumeCouponRequest sdkRequest = new CouponBatchConsumeCouponRequest();
|
|
|
+ sdkRequest.setAccessToken("k23cd*****"); //token
|
|
|
+ sdkRequest.setAppId("6CI1eVeyPQ"); //
|
|
|
+ sdkRequest.setConsumeOutNo("sYCkFmcYFa");
|
|
|
+ sdkRequest.setConsumeTime(1l);
|
|
|
+ sdkRequest.setOpenId("kbJyRUxJCH");
|
|
|
+ sdkRequest.setOrderId("kMrlcAi3dE");
|
|
|
+ CouponBatchConsumeCouponResponse sdkResponse = client.CouponBatchConsumeCoupon(sdkRequest);
|
|
|
+ } catch (TeaException e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+}
|