java开发微信订阅号群发功能(一)

java开发微信订阅号群发功能(一)

在进行订阅号的群发功能时第一步就是要获取access_token这个在之后的接口中都要用到这个参数


以下是access_token的介绍:
在这里插入图片描述

获取access_token的接口地址参数返回值如下:
在这里插入图片描述
链接地址:
“https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET”;


拿到公众号的APPIDAPPSECRET之后就可以进行接口的调用接口获取access_token

  public Map<String, String> getToken( Integer page,Integer size,User user) throws Exception {


        Map<String, String> param = new HashMap<String, String >();
        // TODO code application logic here
        //访问地址
        String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
//
//        //获取Session用户信息
//        //获取APPID、APPSECRET
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//
//        UserResult userResult = (UserResult) request.getSession().getAttribute("userResult");

//        String phone = "12312331";
//        //appid
          String APPID = "123545";
          String APPSECRET = "41135146545165465";
//        String phone = userResult.getPhone();
        //appid
//        String APPID =user.getName();
//        //appsecret
//
//        String APPSECRET = user.getPasword();

        String access_tocken = null;
        String expires_in = null;

        //拼接请求
        String request_url = TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
        HttpsUtil httpsUtil = new HttpsUtil();
        System.out.println(request_url);
        int i = 0;

        //发送请求
        JSONObject jsonObject = httpsUtil.HttpsUtil(request_url,"GET",null);
        System.out.println(jsonObject.toString());

        //添加json白名单
        if (!jsonObject.has("access_token")){
            jsonObject.put("access_token", "");
        }
        if (!jsonObject.has("expires_in")){
            jsonObject.put("expires_in", "");
        }
        //展示access_token
        if(null != jsonObject){
            access_tocken = jsonObject.getString("access_token");
            expires_in = jsonObject.getString("expires_in");
            //获取到的access_tocken值可以写入到文本文件中供其他业务逻辑调用,实例中只打印了没有保存到文件
            System.out.println("TOKENTOKEN"+access_tocken);
            System.out.println("expires_in"+expires_in);
//                i=Integer.parseInt(expires_in);
        }
//        param.put("phone",phone);
        param.put("access_token",access_tocken);
        return param;
    }

在获取access_token时需要传递的参数是需要转换json的在代码注释中有体现

发送请求的工具类如下:

package com.zm.mall.util.wechatutil;

/**
 * Creat0ed by Administrator on 2020/4/29.
 */

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.HttpURLConnection;
import javax.net.ssl.HttpsURLConnection;

import net.sf.json.JSONObject;
public class HttpsUtil {
    /**
     * HttpsUtil方法https请求结果返回蔚json类型
     * @param Url http请求地址
     * @param Method http请求类型支持POST GET
     * @param Output
     * @return InputStream转换成JSONObject后返回
     * @throws Exception
     */
    public JSONObject HttpsUtil(String Url,String Method,String Output) throws Exception{
        JSONObject jsonObject = null;
        URL conn_url =  new URL(Url);
        HttpURLConnection conn = (HttpsURLConnection)conn_url.openConnection();
        conn.setRequestMethod(Method);
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.connect();
        //output获取access_token是不会用到
//        if(Output != null){
//            OutputStream  outputstream =conn.getOutputStream();
//            //字符集,防止出现中文乱码
//            outputstream.write(Output.getBytes("UTF-8"));
//            outputstream.close();
//        }
        //正常返回代码为200
        if(conn.getResponseCode()==200){
            InputStream stream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(stream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            stream.close();
            conn.disconnect();
            jsonObject = JSONObject.fromObject(buffer.toString());
        }
        System.out.println(conn.getResponseCode());
        return jsonObject;
    }
}

这样就可以获取到access_token了

至于怎么获取APPID和APPSECRET可以看看这个链接:
https://www.jb51.net/yunying/166336.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值