|
@@ -1,5 +1,6 @@
|
|
package org.jeecg.modules.ctop.controller;
|
|
package org.jeecg.modules.ctop.controller;
|
|
|
|
|
|
|
|
+import cn.com.ctop.common.utils.Check;
|
|
import cn.com.ctop.common.utils.HttpUtils;
|
|
import cn.com.ctop.common.utils.HttpUtils;
|
|
import cn.com.ctop.common.utils.PropertiesUtils;
|
|
import cn.com.ctop.common.utils.PropertiesUtils;
|
|
import cn.com.ctop.toutiao.common.BytedanceInterfaceConstant;
|
|
import cn.com.ctop.toutiao.common.BytedanceInterfaceConstant;
|
|
@@ -10,6 +11,7 @@ import org.apache.commons.lang.StringUtils;
|
|
import org.jeecg.modules.ctop.entity.CTopOauthToken;
|
|
import org.jeecg.modules.ctop.entity.CTopOauthToken;
|
|
import org.jeecg.modules.ctop.mapper.CTopOauthTokenMapper;
|
|
import org.jeecg.modules.ctop.mapper.CTopOauthTokenMapper;
|
|
import org.jeecg.modules.ctop.service.ICTopOauthTokenService;
|
|
import org.jeecg.modules.ctop.service.ICTopOauthTokenService;
|
|
|
|
+import org.jeecg.modules.ctop.service.IBindAccountService;
|
|
import org.jeecg.modules.kuaishou.service.IKuaishouInterfaceService;
|
|
import org.jeecg.modules.kuaishou.service.IKuaishouInterfaceService;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -22,8 +24,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
|
-import java.net.URLEncoder;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import java.util.UUID;
|
|
@@ -34,47 +34,54 @@ public class AuthController {
|
|
private static final Logger logger = LoggerFactory.getLogger(AuthController.class);
|
|
private static final Logger logger = LoggerFactory.getLogger(AuthController.class);
|
|
@Autowired
|
|
@Autowired
|
|
private IKuaishouInterfaceService kuaishouInterfaceService;
|
|
private IKuaishouInterfaceService kuaishouInterfaceService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private IBindAccountService bindAccountService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private CTopOauthTokenMapper cTopOauthTokenMapper;
|
|
|
|
+
|
|
@RequestMapping("/begin")
|
|
@RequestMapping("/begin")
|
|
public String authBegin(HttpServletRequest request,
|
|
public String authBegin(HttpServletRequest request,
|
|
HttpServletResponse response,
|
|
HttpServletResponse response,
|
|
- @RequestParam("auth_type")String authType){
|
|
|
|
- try{
|
|
|
|
|
|
+ @RequestParam("auth_type") String authType) {
|
|
|
|
+ try {
|
|
String codeUrl = "";
|
|
String codeUrl = "";
|
|
- switch (authType)
|
|
|
|
- {
|
|
|
|
|
|
+ switch (authType) {
|
|
case "bytedance":
|
|
case "bytedance":
|
|
- codeUrl = this.getByteDanceCodeUrl(UUID.randomUUID().toString());
|
|
|
|
|
|
+ codeUrl = bindAccountService.getByteDanceCodeUrl(UUID.randomUUID().toString());
|
|
System.out.println(codeUrl);
|
|
System.out.println(codeUrl);
|
|
- return "redirect:"+codeUrl;
|
|
|
|
|
|
+ return "redirect:" + codeUrl;
|
|
case "kuaishou":
|
|
case "kuaishou":
|
|
- codeUrl = this.getKuaishouCodeUrl(UUID.randomUUID().toString());
|
|
|
|
|
|
+ codeUrl = bindAccountService.getKuaishouCodeUrl(UUID.randomUUID().toString());
|
|
System.out.println(codeUrl);
|
|
System.out.println(codeUrl);
|
|
- return "redirect:"+codeUrl;
|
|
|
|
|
|
+ return "redirect:" + codeUrl;
|
|
default:
|
|
default:
|
|
System.out.println("");
|
|
System.out.println("");
|
|
|
|
|
|
}
|
|
}
|
|
- }catch (Exception e){
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
return "auth_error";
|
|
return "auth_error";
|
|
}
|
|
}
|
|
|
|
+
|
|
@RequestMapping("/bytedance")
|
|
@RequestMapping("/bytedance")
|
|
@ResponseBody
|
|
@ResponseBody
|
|
public String bytedance(HttpServletRequest request,
|
|
public String bytedance(HttpServletRequest request,
|
|
HttpServletResponse response,
|
|
HttpServletResponse response,
|
|
- @RequestParam("auth_code")String authCode,
|
|
|
|
- @RequestParam("state")String state,@RequestParam("accountId")String accountId) {
|
|
|
|
|
|
+ @RequestParam("auth_code") String authCode,
|
|
|
|
+ @RequestParam("state") String state, @RequestParam("accountId") String accountId) {
|
|
System.out.println(request.getQueryString());
|
|
System.out.println(request.getQueryString());
|
|
- if(StringUtils.isNotBlank(authCode)){
|
|
|
|
- System.out.println(this.getByteDanceAccessToken(authCode,accountId));
|
|
|
|
|
|
+ if (StringUtils.isNotBlank(authCode)) {
|
|
|
|
+ System.out.println(this.getByteDanceAccessToken(authCode, accountId, state));
|
|
return "auth_success";
|
|
return "auth_success";
|
|
}
|
|
}
|
|
return "auth_fail";
|
|
return "auth_fail";
|
|
}
|
|
}
|
|
|
|
|
|
@GetMapping("bytedance/refreshtoken")
|
|
@GetMapping("bytedance/refreshtoken")
|
|
|
|
+ public String getByteDanceAccessToken(String authCode, String accountId, String state) {
|
|
@ResponseBody
|
|
@ResponseBody
|
|
public Map<String, Object> refreshToken(String accountId) {
|
|
public Map<String, Object> refreshToken(String accountId) {
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
@@ -113,24 +120,33 @@ public class AuthController {
|
|
private ICTopOauthTokenService tokenService;
|
|
private ICTopOauthTokenService tokenService;
|
|
public String getByteDanceAccessToken(String authCode, String accountId) {
|
|
public String getByteDanceAccessToken(String authCode, String accountId) {
|
|
Map<String, Object> param = new HashMap<String, Object>();
|
|
Map<String, Object> param = new HashMap<String, Object>();
|
|
- param.put("app_id",PropertiesUtils.getValue("bytedance_config","bytedance_appid"));
|
|
|
|
- param.put("secret",PropertiesUtils.getValue("bytedance_config","bytedance_secret"));
|
|
|
|
- param.put("grant_type","auth_code");
|
|
|
|
- param.put("auth_code",authCode);
|
|
|
|
|
|
+ param.put("app_id", PropertiesUtils.getValue("bytedance_config", "bytedance_appid"));
|
|
|
|
+ param.put("secret", PropertiesUtils.getValue("bytedance_config", "bytedance_secret"));
|
|
|
|
+ param.put("grant_type", "auth_code");
|
|
|
|
+ param.put("auth_code", authCode);
|
|
|
|
|
|
Gson gson = new Gson();
|
|
Gson gson = new Gson();
|
|
String result = "";
|
|
String result = "";
|
|
- try{
|
|
|
|
- System.out.println(PropertiesUtils.getValue("bytedance_config","bytedance_api_url")+ BytedanceInterfaceConstant.AUTH_TOKEN);
|
|
|
|
- result = HttpUtils.httpPostRequest(PropertiesUtils.getValue("bytedance_config","bytedance_api_url")+BytedanceInterfaceConstant.AUTH_TOKEN,param,new HashMap<>());
|
|
|
|
- System.out.println(result);
|
|
|
|
|
|
+ try {
|
|
|
|
+ result = HttpUtils.httpPostRequest(PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + BytedanceInterfaceConstant.AUTH_TOKEN, param, new HashMap<>());
|
|
JSONObject resultObject = JSONObject.parseObject(result);
|
|
JSONObject resultObject = JSONObject.parseObject(result);
|
|
Integer code = resultObject.getInteger("code");
|
|
Integer code = resultObject.getInteger("code");
|
|
String message = resultObject.getString("message");
|
|
String message = resultObject.getString("message");
|
|
- if(null == code||code!=0){
|
|
|
|
- logger.info("获取token失败,accountId:{},message:{}",accountId,message);
|
|
|
|
|
|
+ if (null == code || code != 0) {
|
|
|
|
+ logger.info("获取token失败,accountId:{},message:{}", accountId, message);
|
|
}
|
|
}
|
|
JSONObject data = resultObject.getJSONObject("data");
|
|
JSONObject data = resultObject.getJSONObject("data");
|
|
|
|
+ CTopOauthToken token = new CTopOauthToken(accountId, data);
|
|
|
|
+ if (Check.isNull(token)) {
|
|
|
|
+ Map<String, Object> deleteMap = new HashMap<>();
|
|
|
|
+ deleteMap.put("advertiser_id", state);
|
|
|
|
+ deleteMap.put("account_id", token.getAccountId());
|
|
|
|
+ cTopOauthTokenMapper.deleteByMap(deleteMap); //删除 广告主id下的相关授权信息
|
|
|
|
+ cTopOauthTokenMapper.insert(token);
|
|
|
|
+ bindAccountService.addBindAccount(token.getAccountId(), KuaishouInterfaceConstant.TYPE_AUTHORIZATION, state, KuaishouInterfaceConstant.LOGIN_TYPE_BYTEDANCE); //账号绑定
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
CTopOauthToken token = new CTopOauthToken(accountId,data);
|
|
CTopOauthToken token = new CTopOauthToken(accountId,data);
|
|
cTopOauthTokenMapper.deleteById(token.getId());
|
|
cTopOauthTokenMapper.deleteById(token.getId());
|
|
cTopOauthTokenMapper.insert(token);
|
|
cTopOauthTokenMapper.insert(token);
|
|
@@ -140,28 +156,5 @@ public class AuthController {
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private CTopOauthTokenMapper cTopOauthTokenMapper;
|
|
|
|
-
|
|
|
|
- public String getKuaishouCodeUrl(String state) throws UnsupportedEncodingException {
|
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
|
- sb.append(PropertiesUtils.getValue("kuaishou_config","kuaishou_auth_url"))
|
|
|
|
- .append(KuaishouInterfaceConstant.AUTH_PATH)
|
|
|
|
- .append("?app_id="+PropertiesUtils.getValue("kuaishou_config","kuaishou_appid"))
|
|
|
|
- .append("&state="+state)
|
|
|
|
- .append("&scope=%5B%22report_service%22%2C%22ad_query%22%2C%22ad_manage%22%5D")
|
|
|
|
- .append("&redirect_uri="+ URLEncoder.encode(PropertiesUtils.getValue("kuaishou_config","kuaishou_callback_url"),"UTF-8"));
|
|
|
|
- return sb.toString();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
|
|
- public String getByteDanceCodeUrl(String state) throws UnsupportedEncodingException {
|
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
|
- sb.append(PropertiesUtils.getValue("bytedance_config","bytedance_auth_url"))
|
|
|
|
- .append(BytedanceInterfaceConstant.AUTH_PATH)
|
|
|
|
- .append("?app_id="+PropertiesUtils.getValue("bytedance_config","bytedance_appid"))
|
|
|
|
- .append("&state="+state)
|
|
|
|
- .append("&redirect_uri="+ URLEncoder.encode(PropertiesUtils.getValue("bytedance_config","bytedance_callback_url"),"UTF-8"));
|
|
|
|
- return sb.toString();
|
|
|
|
- }
|
|
|
|
}
|
|
}
|