java spring boot 获取小程序 accessToken

  // 小程序唯一标识 (在微信小程序管理后台获取)
    @Value("${weixin.app_id}") // spring配置文件配置了appID
    private String appId;
    // 小程序的 app secret (在微信小程序管理后台获取)
    @Value("${weixin.app_secret}") // spring配置文件配置了secret
    private String secret;
    //    每3分钟启动
    @Scheduled(cron = "0 0/3 * * * ?")
    public void timerToNow(){
        System.out.println("定时任务启动了");
        //这里直接写死就可以,不用改,用法可以去看api
        String grant_type="client_credential";
        //封装请求数据
        String params = "grant_type=" + grant_type + "&secret=" + secret + "&appid="+ appId;
        //发送GET请求
        String sendGet = new HttpUtils().get("https://api.weixin.qq.com/cgi-bin/token?"+params);
        // 解析相应内容(转换成json对象)
        JsonObject json = new Gson().fromJson(sendGet,JsonObject.class);
        //拿到accesstoken
        AccessToken accessToken = new AccessToken();
        accessToken.setId(1);
        accessToken.setAccessToken(json.get("access_token").getAsString());
        //下面的操作我是存放到数据库了
        accessTokenDao.save(accessToken);
        System.out.println("定时任务结束了");
    }

猜你喜欢

转载自blog.csdn.net/a961011576/article/details/85098786