org.apache.httpcomponents
httpclient
4.5.13
org.apache.httpcomponents
httpcore
com.github.binarywang
weixin-java-mp
4.1.0
PC端后台扫码登录~获取ticket及其他参数
@GetMapping("/getTicket")
public ResponseData
application.yml配置
### 微信网页(h5)配置参数 wxh5: appid: wx57f8a596522dfb99 #公众号APPID (这使用的是测试账户公众号) appSecret: 426c9109b51dd1f5183ec506a7d78dbd #公众号AppSecret (这使用的是测试账户公众号) serverurl: http://jdqaiv.natappfree.cc #服务器回调地址(最好是https协议)实现类
@Value("${wxh5.appid}")
private String appid;
@Value("${wxh5.appSecret}")
private String appSecret;
@Value("${wxh5.serverurl}")
private String serverurl;
@Override
public ResponseData
工具类
public static MapHttpClientgetTicket(String appid, String appSecret) { String accessToken = getAccessToken(appid, appSecret); String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + accessToken.trim(); JSONObject jsonObject = new JSONObject(); JSONObject actionInfo = new JSONObject(); JSONObject scene = new JSONObject(); scene.put("scene_str", getRandomString(20)); actionInfo.put("scene", scene); jsonObject.put("expire_seconds", "604800"); jsonObject.put("action_name", "QR_STR_SCENE"); jsonObject.put("action_info", actionInfo); String json = HttpClient.sendJson(url, jsonObject.toString()); JSONObject resJson = JSONObject.parseObject(json); Map map = new HashMap<>(); map.put("ticket", resJson.get("ticket").toString()); map.put("expire_seconds", resJson.get("expire_seconds").toString()); map.put("url", resJson.get("url").toString()); return map; }
public static String sendJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
获取二维码
通过 ticket 换取二维码
获取二维码 ticket 后,开发者可用 ticket 换取二维码图片。请注意,本接口无须登录态即可调用。
请求说明
HTTP GET请求(请使用 https 协议)https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET 提醒:TICKET记得进行UrlEncode
前端获取即可
微信公众号测试平台:测试平台
@PostMapping("/qrCodeChekToken")
public ResponseData
实现类
@Override public ResponseData
获取到信息之后就可以做其他操作了,这里的实现使用的是微信公众平台方式实现,也可以使用微信开放平台实现,但是开放平台需要认证为开发者资质。



