因为我要做群发功能,所以我要获取关注某个微信公众号的所有用户的openid
前提
1、有微信公众号(appid与秘钥)
2、配置网页授权的域名信息
3、申请模板,有模板id
4、配置服务器,把域名ip地址添加到白名单
服务器配置,需要把项目代码部署到服务器上,才能配置,不然会提示错误!
配置服务器的代码如下
package com.example.demo.control;
import com.example.demo.token.SignUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/weixin")
public class TokenControl {
/**
* 微信消息接收和token验证
*/
@RequestMapping(value = "/token", method = RequestMethod.GET)
public String get(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("----------------验证微信服务号信息开始----------");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 微信加密签名
String signature = request.getParameter("signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
if (SignUtil.checkSignature(signature, timestamp, nonce)) {
System.out.println("----验证服务号结束.........");
return echostr;
}else{
return null;
}
}//public
}
package com.example.demo.token;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class SignUtil {
private static String token = "token";
/**
* 校验签名
* @pa