java开发微信订阅号群发功能(一)
在进行订阅号的群发功能时第一步就是要获取access_token这个在之后的接口中都要用到这个参数
以下是access_token的介绍:
获取access_token的接口地址参数返回值如下:
链接地址:
“https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET”;
拿到公众号的APPID和APPSECRET之后就可以进行接口的调用接口获取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