|
|
@@ -1,4 +1,4 @@
|
|
|
-package com.fs.app.integration.client.advertiser;
|
|
|
+package com.fs.newAdv.integration.client.advertiser;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
@@ -6,26 +6,19 @@ import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.json.JSONArray;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
-import com.fs.ad.yk.utils.Md5Util;
|
|
|
-import com.fs.app.integration.client.AbstractApiClient;
|
|
|
-import com.fs.app.integration.client.IAccessTokenClient;
|
|
|
-import com.fs.baidu.utils.AESUtils;
|
|
|
+import com.fs.newAdv.integration.client.AbstractApiClient;
|
|
|
+import com.fs.newAdv.integration.client.IAccessTokenClient;
|
|
|
import com.fs.common.constant.SystemConstant;
|
|
|
import com.fs.common.exception.ThirdPartyException;
|
|
|
import com.fs.newAdv.domain.PromotionAccount;
|
|
|
import com.fs.newAdv.domain.SiteStatistics;
|
|
|
import com.fs.newAdv.enums.AdvertiserTypeEnum;
|
|
|
-import com.fs.newAdv.service.IPromotionAccountService;
|
|
|
import com.fs.newAdv.vo.AccessTokenByAuthCodeVo;
|
|
|
import com.fs.newAdv.vo.AccessTokenVo;
|
|
|
-import com.google.gson.Gson;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import javax.crypto.SecretKey;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
|
|
|
@@ -46,19 +39,9 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
*/
|
|
|
private static final String CONVERSION_API_URL = "https://ocpc.baidu.com/ocpcapi/api/uploadConvertData";
|
|
|
private static final String REPORT_DATA_API_URL = "https://api.baidu.com/json/sms/service/OpenApiReportService/getReportData";
|
|
|
-
|
|
|
-
|
|
|
- private static final String APPID = "appId";
|
|
|
- private static final String AUTH_CODE = "authCode";
|
|
|
- private static final String USERID = "userId";
|
|
|
- private static final String TIMESTAMP = "timestamp";
|
|
|
- private static final String SIGNATURE = "signature";
|
|
|
- private static final String STATE = "state";
|
|
|
private static final String ACCESSTOKEN_URL = "https://u.baidu.com/oauth/accessToken";
|
|
|
- private static final String AES_OFFSET = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
|
|
|
+ private static final String REFRESHTOKEN_URL = "https://u.baidu.com/oauth/refreshToken";
|
|
|
|
|
|
- @Autowired
|
|
|
- private IPromotionAccountService promotionAccountService;
|
|
|
|
|
|
/**
|
|
|
* 回传转化数据到百度
|
|
|
@@ -183,127 +166,38 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public Map<String, String> refreshAccessToken(String appId, String appSecret, String refreshToken) {
|
|
|
- return Collections.emptyMap();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public AccessTokenVo getAccessTokenByAuthCode(AccessTokenByAuthCodeVo codeVo) {
|
|
|
+ public AccessTokenVo refreshAccessToken(String appId, String appSecret, String refreshToken) {
|
|
|
// 调用接口换取授权令牌
|
|
|
- Map<String, Object> response = getBaiDuAccessToken(codeVo);
|
|
|
- log.info("callback:getAccesstoken, response={}", response);
|
|
|
- String accessToken = null;
|
|
|
- String refreshToken = null;
|
|
|
-
|
|
|
- if (!response.isEmpty()) {
|
|
|
- JSONObject res = new JSONObject(response);
|
|
|
- int code = (int) res.get("code");
|
|
|
- if (code == 0) {
|
|
|
- JSONObject data = res.getJSONObject("data");
|
|
|
- accessToken = (String) data.get("accessToken");
|
|
|
- refreshToken = (String) data.get("refreshToken");
|
|
|
- return AccessTokenVo.builder()
|
|
|
- .accessToken(accessToken)
|
|
|
- .refreshToken(refreshToken)
|
|
|
- .build();
|
|
|
- }
|
|
|
+ Map<String, Object> requestMap = new HashMap<>();
|
|
|
+ requestMap.put("appId", appId);
|
|
|
+ requestMap.put("secretKey", appSecret);
|
|
|
+ requestMap.put("refreshToken", refreshToken);
|
|
|
+ // requestMap.put("userId", userId);
|
|
|
+ String paramsJson = JSONUtil.toJsonStr(requestMap);
|
|
|
+ HttpResponse execute = HttpRequest.post(REFRESHTOKEN_URL)
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .body(paramsJson)
|
|
|
+ .timeout(SystemConstant.API_TIMEOUT)
|
|
|
+ .execute();
|
|
|
+ JSONObject res = new JSONObject(execute.body());
|
|
|
+ int code = (int) res.get("code");
|
|
|
+ if (code == 0) {
|
|
|
+ JSONObject data = res.getJSONObject("data");
|
|
|
+ return AccessTokenVo.builder()
|
|
|
+ .accessToken(data.getStr("accessToken"))
|
|
|
+ .refreshToken(data.getStr("refreshToken"))
|
|
|
+ .build();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 填充请求参数,开发者可用实体代替map
|
|
|
- *
|
|
|
- * @param params
|
|
|
- * @param request
|
|
|
- */
|
|
|
- private void fillParams(Map<String, String> params, AccessTokenByAuthCodeVo request) {
|
|
|
- params.put(APPID, request.getAppId());
|
|
|
- params.put(AUTH_CODE, request.getAuthCode());
|
|
|
- params.put(USERID, request.getUserId());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 校验状态码是否符合预期
|
|
|
- *
|
|
|
- * @param appId 应用ID
|
|
|
- * @param userId 创建应用的ID
|
|
|
- * @param state 请求参数中的状态码
|
|
|
- * @return
|
|
|
- */
|
|
|
- private boolean checkState(String appId, Long userId, String state) {
|
|
|
- // md5util 开发者自行开发即可
|
|
|
- String newState = Md5Util.MD5(appId.concat("_").concat(String.valueOf(userId)));
|
|
|
- return newState.toLowerCase().equals(state);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 封装返回json串
|
|
|
- *
|
|
|
- * @param code
|
|
|
- * @param message
|
|
|
- * @param data
|
|
|
- * @return
|
|
|
- */
|
|
|
- private String getResponseJson(int code, String message, Object data) {
|
|
|
- // 封装返回字段的map
|
|
|
- Map<String, Object> responseMap = new HashMap<>();
|
|
|
- responseMap.put("code", code);
|
|
|
- responseMap.put("message", message);
|
|
|
- responseMap.put("data", data);
|
|
|
- return org.json.JSONObject.valueToString(responseMap);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 签名验证方法
|
|
|
- *
|
|
|
- * @param params
|
|
|
- * @param sk
|
|
|
- * @return
|
|
|
- */
|
|
|
- private boolean checkSignature(Map<String, String> params, String sk) {
|
|
|
- // 获取验签字符串:按key自然排序,拼接成 json 字符串,
|
|
|
- // 示例:str1 = {"appId": xxxxx, "authCode": xxx, "state": xxx,"timestamp": xxx}
|
|
|
- TreeMap<String, Object> map = new TreeMap<>();
|
|
|
- map.put(APPID, params.get(APPID));
|
|
|
- map.put(AUTH_CODE, params.get(AUTH_CODE));
|
|
|
- map.put(USERID, params.get(USERID));
|
|
|
- map.put(STATE, params.get(STATE));
|
|
|
- map.put(TIMESTAMP, params.get(TIMESTAMP));
|
|
|
- // 根据上述签名算法对接收到的参数签名
|
|
|
- String sign = null;
|
|
|
- try {
|
|
|
- sign = paramsSign(sk, map);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- log.info("callback: signature = {}", sign);
|
|
|
- // 判断签名和URL传参签名是否一致
|
|
|
- return params.get(SIGNATURE).equals(sign);
|
|
|
- }
|
|
|
-
|
|
|
- public String paramsSign(String secretKey, TreeMap<String, Object> map) throws Exception {
|
|
|
-
|
|
|
- // 拼接成 json 字符串, 保证生成的json串字段顺序和Map中的顺序一致即可
|
|
|
- String json = new Gson().toJson(map); // base64编码
|
|
|
- byte[] bytes = Base64.getEncoder()
|
|
|
- .encodeToString(json.getBytes(StandardCharsets.UTF_8))
|
|
|
- .getBytes(StandardCharsets.UTF_8);
|
|
|
- // AES加密 密钥为 应用sk 的前16位字符
|
|
|
- SecretKey keyAES = AESUtils.loadKeyAES(secretKey.substring(0, 16));
|
|
|
- return AESUtils.encryptAES(bytes, keyAES, AES_OFFSET);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 换取授权令牌
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- private Map<String, Object> getBaiDuAccessToken(AccessTokenByAuthCodeVo codeVo) {
|
|
|
+ @Override
|
|
|
+ public AccessTokenVo getAccessTokenByAuthCode(AccessTokenByAuthCodeVo codeVo) {
|
|
|
+ // 调用接口换取授权令牌
|
|
|
Map<String, Object> requestMap = new HashMap<>();
|
|
|
- requestMap.put(APPID, codeVo.getAppId());
|
|
|
+ requestMap.put("appId", codeVo.getAppId());
|
|
|
requestMap.put("secretKey", codeVo.getAppSecret());
|
|
|
- requestMap.put(AUTH_CODE, codeVo.getAuthCode());
|
|
|
+ requestMap.put("authCode", codeVo.getAuthCode());
|
|
|
requestMap.put("grantType", "access_token");
|
|
|
requestMap.put("userId", codeVo.getUserId());
|
|
|
String paramsJson = JSONUtil.toJsonStr(requestMap);
|
|
|
@@ -313,7 +207,17 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
.body(paramsJson)
|
|
|
.timeout(SystemConstant.API_TIMEOUT)
|
|
|
.execute();
|
|
|
- return JSONUtil.parseObj(execute.body());
|
|
|
+
|
|
|
+ JSONObject res = new JSONObject(execute.body());
|
|
|
+ int code = (int) res.get("code");
|
|
|
+ if (code == 0) {
|
|
|
+ JSONObject data = res.getJSONObject("data");
|
|
|
+ return AccessTokenVo.builder()
|
|
|
+ .accessToken(data.getStr("accessToken"))
|
|
|
+ .refreshToken(data.getStr("refreshToken"))
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|
|
|
|