广点通数据拉取

广点通的广告数据拉取
首先设计一个接口用于授权回调,授权流程如下,
https://developers.e.qq.com/oauth/authorize?client_id=<CLIENT_ID>&redirect_uri=https://www.example.com/response&state=&scope=
在官方文档你会看到这样的授权说明,
client_id 用户id
redirect_uri回调地址
整个流程就是,你拼好参数直接在浏览器访问回弹出一个回调地址https://www.example.com/response&state=&scope=
访问写好的的地址

设计一个接口

@RequestMapping("/tx/oauth/callback")
	@ResponseBody
	@Menu("广点通初始化回调")
	public Result repairCostgdt(String authorization_code, String state){
		return gdtService.oauth(authorization_code,state);
	}

service层,利用redis等缓存工具去存回调的authorization_code

 /*回调authorization_code*/
    public Result oauth(String authorization_code,String state) {
        System.out.println("@@@@authorization_code"+authorization_code);
        RedisCacheUtils.set("authorization_code",authorization_code);
        gettoken(authorization_code);
        return Result.SUCCESS;
    }

接下来是获取token,同意的道理是存到redis
得到access_token的同时得到一个gdt_refresh_token

 /*第一次授权 获取token和resrefresh_token,已经完成*/
    public boolean gettoken(String authorization_code) {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("https://api.e.qq.com/oauth/token?");
        stringBuffer.append("client_id="+client_id+"&");
        stringBuffer.append("client_secret="+client_secret+"&");
        stringBuffer.append("grant_type=authorization_code&");
        stringBuffer.append("redirect_uri=http://opa.i-fu&");
        stringBuffer.append("authorization_code="+从reids拿authorization_code);
        String jsonObject=HttpUtils.get(stringBuffer.toString());
        JSONObject jsonObject1=(JSONObject) JSONObject.parse(jsonObject);
        RedisCacheUtils.set("gdt_access_token",jsonObject1.getString("access_token"));
        RedisCacheUtils.set("gdt_refresh_token",jsonObject1.getString("refresh_token"));
        return true;
    }

如果你的token设置了过时记得去刷新,用reftoken,刷新后的token要存到redis

 /*刷新token,呆开启定时器时调用*/
    public void resreftoken(){
        String resf=String.valueOf(RedisCacheUtils.get("gdt_refresh_token"));
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("https://api.e.qq.com/oauth/token?");
        stringBuffer.append("client_id="+client_id+"&");
        stringBuffer.append("client_secret="+client_secret+"&");
        stringBuffer.append("grant_type=refresh_token&");
        stringBuffer.append("refresh_token="+resf);
        String jsonObject=HttpUtils.get(stringBuffer.toString());
        JSONObject jsonObject1=(JSONObject) JSONObject.parse(jsonObject);
        JSONObject data=(JSONObject) JSONObject.parse(jsonObject1.getString("data"));
        RedisCacheUtils.set("gdt_access_token",data.getString("access_token"));
    }

接下来就是来取数据

设置公共参数

/*公共参数*/
   public String common(String url){
       StringBuffer stringBuffer=new StringBuffer(url);
       String access_token=String.valueOf(RedisCacheUtils.get("gdt_access_token"));
       String timestamp=String.valueOf(System.currentTimeMillis()/1000);
       String nonce= UUID.randomUUID().toString().trim().replaceAll("-", "");
       String [] fields={"cost","cpc","view_count"};
       stringBuffer.append("access_token="+access_token+"&");
       stringBuffer.append("nonce="+nonce+"&");
       stringBuffer.append("timestamp="+String.valueOf(System.currentTimeMillis()/1000));
       System.out.println("共同参数和api"+stringBuffer.toString());
       return stringBuffer.toString();
   }

进行第一次来取,目的是为了拉取page页码

 /*第一轮拉数据.目的是为了拉到总页码*/
    public JSONObject getpagedata(String statdata,String enddata,int page,String url,String account_id ){
            Map map1=new HashMap();
            map1.put("account_id",account_id);
            map1.put("level","REPORT_LEVEL_ADGROUP");
            map1.put("date_range", "{\"start_date\":\""+statdata+"\",\"end_date\":\""+statdata+"\"}");
            map1.put("page_size","1000");
            map1.put("page",String.valueOf(page));
            map1.put("group_by","[\"hour\",\"adgroup_id\"]");
            map1.put("fields","[\"account_id\",\"ad_name\",\"valid_click_count\",\"view_count\",\"cpc\",\"cost\",\"web_register_count\",\"click_activated_rate\",\"app_register_count\",\"ad_id\",\"hour\",\"adgroup_id\",\"adgroup_name\"]" );
            String path=common(url);
            String res= HttpUtils.get(path,map1);
            System.out.println(res);
            JSONObject result = (JSONObject) JsonUtils.parseObject(res);
            System.out.println("查询结果为"+result.toString());
            return result;
    }
  /*拉取,一小时拉,*/
    public void  getdata(String statdata,String enddata,String url){
        List<Map<String,Object>> list=getDao().queryMapList("SELECT * FROM `lw_gdt_info`");
        for (int j = 0; j < list.size(); j++) {
            JSONObject jsonObject=getpagedata(statdata,enddata,1,url,String.valueOf(list.get(j).get("account_id")));
            if(null !=jsonObject){
                String code=jsonObject.getString("code");
                if("0".equals(code)) {
                    JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.getString("data"));
                    JSONObject jsonObject2= (JSONObject) JSONObject.parse(jsonObject1.getString("page_info"));
                    int total = Integer.valueOf(jsonObject2.getString("total_page"));
                    if (total > 1) {
                        /*对总页码进行循环查询*/
                        for (int i = 1; i <=total; i++) {
                            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Date date = new Date(System.currentTimeMillis());


                            /*从返回值中拿当前小时,如果等于当前小时-1,如果为零的时候特别处理*/
                            if(date.getHours()==0){
                                Date today = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                adddaydata(simpleDateFormat.format(today),simpleDateFormat.format(today),"https://api.e.qq.com/v1.1/hourly_reports/get?",23);
                            }else {
                                List<Map<String,Object>> list1=(List) JSONObject.parse(jsonObject1.getString("list"));
                                System.out.println(list.size());
                                System.out.println(list.toString());
                                JSONObject jsonObject3 = getpagedata(statdata,enddata, i,url,String.valueOf(list.get(j).get("account_id")));
                                JSONObject jsonObject4 = JSONObject.parseObject(jsonObject3.getString("data"));
                                List<Map<String,Object>> list2=(List) JSONObject.parse(jsonObject4.getString("list"));
                                insertPaybuyHour(list2,statdata,date.getHours()-1); }
                        }
                    } else {
                        /*直接入库*/
                        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date = new Date(System.currentTimeMillis());
                        /*零小时特别处理*/
                        if(date.getHours()==0){
                            Date today = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                            adddaydata(simpleDateFormat.format(today),simpleDateFormat.format(today),"https://api.e.qq.com/v1.1/hourly_reports/get?",23);

                        }else {
                            List<Map<String,Object>> list1=(List) JSONObject.parse(jsonObject1.getString("list"));
                            {
                                insertPaybuyHour(list1,statdata,date.getHours()-1);
                            }
                        }
                    }
                }else { System.out.println("查询接口出错"); }
            }else { System.out.println("没有查到数据"); }
        }



    }

上面就是自动拉取的整个过程

*手动追加数据*/
    public Result adddaydata(String statdata,String enddata,String url,int hour){
        List<Map<String,Object>> list=getDao().queryMapList("SELECT * FROM `lw_gdt_info`");
        /*公共参数*/
        for (int j = 0; j < list.size(); j++) {
        JSONObject jsonObject=getpagedata(statdata,enddata,1,url,String.valueOf(list.get(j).get("account_id")));
        if(null !=jsonObject){
            String code=jsonObject.getString("code");
            if("0".equals(code)) {
                JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.getString("data"));
                JSONObject jsonObject2= (JSONObject) JSONObject.parse(jsonObject1.getString("page_info"));
                int total = Integer.valueOf(jsonObject2.getString("total_page"));
                if (total > 1) {
                    /*对总页码进行循环查询*/
                    for (int i = 1; i <=total; i++) {
                        JSONObject jsonObject3 = getpagedata(statdata,enddata, i,url,String.valueOf(list.get(j).get("account_id")));
                        JSONObject jsonObject4 = JSONObject.parseObject(jsonObject3.getString("data"));
                        List<Map<String,Object>> list1=(List) JSONObject.parse(jsonObject4.getString("list"));
                        if(hour==25){
                            insertPay(list1,statdata);
                        }
                        {
                            insertPaybuyHour(list1,statdata,hour);
                        }
                    }
                } else {
                    /*直接入库*/
                    List<Map<String,Object>> list1=(List) JSONObject.parse(jsonObject1.getString("list"));
                    if(hour==25){
                       //该办法时时你存数据的业务,自己写
                        insertPay(list1,statdata);
                    }
                    {    
                       //该办法时时你存数据的业务,自己写
                        insertPaybuyHour(list1,statdata,hour);
                    }

                }
            }else { System.out.println("查询接口出错"); }
        }else { System.out.println("没有查到数据"); }


    }
        return Result.SUCCESS;
    }

上面的拉取的业务,

追加收动取数据接口

@RequestMapping("/tx/")
	@ResponseBody
	@Menu("广点通手动补充")
	public Result cost(String state,String enddate,int hour){
		/*如果按天拉取数据,hour传25,如果按小时数拉取数据传具体的小时*/
		return gdtService.adddaydata(state,state,"https://api.e.qq.com/v1.1/hourly_reports/get?",hour);
	}

记得开启定时任务,定时拉取

   /*定时刷新token*/
    @Scheduled(cron = "0 10 0/1 ? * *")
    public void reftoken(){

        gdtService.resreftoken();

    }


//    @Scheduled(cron = "0 22 0/1 ? * *")@Scheduled(cron = "0 0/2 * ? * *")
//    /*小时报表*/
    @Scheduled(cron = "0 22 0/1 ? * *")
    @Async
    public void hourdta(){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        gdtService.getdata(formatter.format(date),formatter.format(date),"https://api.e.qq.com/v1.1/hourly_reports/get?");

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tiny(泰尼)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值