-
注册登录 腾讯广告开发平台(默认使用QQ)
-
创建应用
回调地址填写见下
//获取accesstoken的url
@Value("${tencent.token.url:https://api.e.qq.com/oauth/token?client_id=%s&client_secret=%s&grant_type=%s}")
public String TOKEN_URL;
@Value("${tencent.client.id}")
private String CLIENT_ID;
@Value("${tencent.client.secret}")
private String CLIENT_SECRET;
@Value("${envUrl:https://fhgosit-api.newhopedairy.cn}")
private String envUrl;
@Value("${tokenUrl:/open-api/api/tencent/advertising/callback}")
private String tokenUrl;
//刷新类型
public static final String GRANT_TYPE_VALUE_AUTHORIZATION = "authorization_code";//授权码方式获取 token, 这个时候必填redirect_uri
public static final String GRANT_TYPE_VALUE_REFRESH = "refresh_token";//刷新 token
public void refreshToken(String grantTypeValue) {
String authorization_code = request.getParameter(AUTHORIZATION_CODE);
if (GRANT_TYPE_VALUE_AUTHORIZATION.equals(grantTypeValue) && StringUtils.isEmpty(authorization_code)) {
log.error("authorization_code更新token失败!authorization_code为空");
return;
}
String requestUrl = String.format(TOKEN_URL,CLIENT_ID,CLIENT_SECRET,grantTypeValue);
String refresh_token = "";
String access_token = "";
if (GRANT_TYPE_VALUE_AUTHORIZATION.equals(grantTypeValue)) {
requestUrl += "&redirect_uri=" + envUrl+tokenUrl;
requestUrl += "&" + GRANT_TYPE_VALUE_AUTHORIZATION + "=" + authorization_code;
} else {
refresh_token = redisUtils.getCacheObject(RedisKeyConstant.REFRESH_TOKEN);
if (StringUtils.isEmpty(refresh_token)) {//万一refresh_token过期了, 需要运营重新请求调用授权获取
log.error("refresh_token更新token失败! refresh_token已经失效!");
return;
}
requestUrl += "&" + GRANT_TYPE_VALUE_REFRESH + "=" + refresh_token;
}
String returnMsg = HttpClient4Utils.doGet(requestUrl);
log.info(returnMsg);
Map map = JSONObject.parseObject(returnMsg, Map.class);
if (map.get("code") == null || !"0".equals(map.get("code").toString())) {
log.error(grantTypeValue + "更新token失败!" + GRANT_TYPE_VALUE_AUTHORIZATION + "=" + authorization_code + "," + GRANT_TYPE_VALUE_REFRESH + "=" + refresh_token + "错误码=" + map.get("code").toString() + "错误原因=" + map.get("message").toString());
return;
}
Map data = (Map) map.get("data");
access_token = data.get("access_token").toString();
redisUtils.deleteObject(RedisKeyConstant.ACCESS_TOKEN);
redisUtils.setCacheObject(RedisKeyConstant.ACCESS_TOKEN, access_token);
refresh_token = data.get("refresh_token").toString();
redisUtils.deleteObject(RedisKeyConstant.REFRESH_TOKEN);
redisUtils.setCacheObject(RedisKeyConstant.REFRESH_TOKEN, refresh_token);
log.info("更新token成功!");
}
-
访问链接获取token
应用审核通过之后访问 http://developers.e.qq.com/oauth/authorize?client_id=填写刚刚创建的应用id&redirect_uri=填写刚刚创建的controller完整url
-
前端传入gdt_vid
在需要回传数据的页面,由前端从小程序path地址上获取行为参数gdt_vid传入后端
-
组装回传数据
//用户行为上传需要的几个字段
@Value("${wechat.companyIdAppId.4}")
public String WECHAT_APP_ID;
//推广帐号 id,有操作权限的帐号 id,包括代理商和广告主帐号 id
@Value("${tencent.adSponsor}")
public String ACCOUNT_ID;
//用户行为源 id,通过 [user_action_sets 接口] 创建用户行为源时分配的唯一 id
@Value("${tencent.userAction.setsId}")
public String USER_ACTION_SET_ID;
//注册类型-下单
@Value("${tencent.userAction.type:COMPLETE_ORDER}")
public String ACTION_TYPE;
//上传用户行为的url
@Value("${tencent.userAction.url:https://api.e.qq.com/v1.1/user_actions/add?access_token=%s×tamp=%s&nonce=%s}")
public String USER_ACTION_ADD_URL;
/**
* 上传用户行为
*/
@Override
public boolean userActionAdd(UserActionAddReq userActionAddReq) {
log.info("开始进行回调: {}", JSON.toJSONString(userActionAddReq));
Long timeStamp = System.currentTimeMillis() / 1000;
String accessToken = getAccessToken();
String uuid = UUID.randomUUID().toString().trim().replace("-", "");
String clickId = StringUtils.isEmpty(userActionAddReq.getGdtVid()) ? "":userActionAddReq.getGdtVid();
String baseUrl = String.format(USER_ACTION_ADD_URL, accessToken, timeStamp, uuid);
Map<String, Object> actionsMap = Maps.newHashMap();
actionsMap.put("action_time",timeStamp);
actionsMap.put("external_action_id",userActionAddReq.getExternalActionId());
actionsMap.put("outer_action_id",userActionAddReq.getExternalActionId());
Map<Object, Object> userId = new HashMap<>();
userId.put("wechat_openid", userActionAddReq.getOpenId());
userId.put("wechat_app_id", WECHAT_APP_ID);
actionsMap.put("user_id",userId);
if (StringUtils.isNotEmpty(userActionAddReq.getGdtVid())) {
Map<String, Object> trace = new HashMap<>();
trace.put("click_id", clickId);
actionsMap.put("trace",trace);
}
Map<String,Integer> actionParam = Maps.newHashMap();
//订单总金额(分)
actionParam.put("value",userActionAddReq.getTotalAmount());
actionParam.put("total_amount",userActionAddReq.getTotalAmount());
//优惠后的订单总金额(分)
actionParam.put("amount_after_discount",userActionAddReq.getAmountAfterDiscount());
//单价
actionParam.put("unit_price",userActionAddReq.getUnitPrice());
//购买件数
actionParam.put("quantity",userActionAddReq.getQuantity());
actionsMap.put("action_param",actionParam);
ArrayList<Map> actions = new ArrayList<>();
actionsMap.put("action_type",ACTION_TYPE);
actions.add(actionsMap);
Map<String, Object> param = Maps.newHashMap();
param.put("account_id",ACCOUNT_ID);
param.put("user_action_set_id",USER_ACTION_SET_ID);
param.put("actions",actions);
String jsonStr = JSONObject.toJSONString(param);
log.info("组装后上报数据:{}",jsonStr);
String result = HttpClient4Utils.doPost(baseUrl, jsonStr);
Map map = JSONObject.parseObject(result, Map.class);
if (!"0".equals(map.get("code").toString())) {
log.error("上传用户行为失败:" + "openId=" + userActionAddReq.getOpenId() + "&gdtVid=" + userActionAddReq.getGdtVid() + "错误码=" + map.get("code").toString() + "错误原因=" + map.get("message").toString());
return false;
}
Object trace_id = map.get("trace_id");
log.info("{}上传用户行为成功:{}",userActionAddReq.getExternalActionId(),trace_id);
//保存trace_id //DataNexus 赋予的唯一 id 值,供排查问题使用,若咨询反馈中心请提供该字段的值
redisUtils.setCacheObject(RedisKeyConstant.USER_ACTION_TRACEID+userActionAddReq.getExternalActionId(), trace_id);
return true;
}
参考来源: