官方文档https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html
本文使用B和C接口。
(1)获取token
/**
* 获取access_token<br>
* https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183
*
* @author ==
* @date 2019年5月7日 上午11:36:39
* @return
*/
public static String getToken() {
// https请求方式: GET
String appid = "==";
String secret = "==";
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + "&appid=" + appid
+ "&secret=" + secret;
String r = HttpUtil.getHttp(url);
String access_token = JSONObject.parseObject(r).getString("access_token");
return access_token;
}
(2)接口B
public static void getCode1(String access_token, String imgFileName) {
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;
Map<String, String> param = new HashMap<String, String>();
param.put("scene", "a=0");
// param.put("access_token", access_token);
param.put("page", "pages/index/index");
try {
HttpUtil.postDataWithJson(url, param, imgFileName);
log.info("下载小程序码,保存到文件:" + imgFileName);
} catch (IOException e) {
e.printStackTrace();
log.error(e);
}
}
(3)接口C 小程序二维码
/**
* 获取小程序二维码(不推荐使用)<br>
* 接口C<br>
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/wxacode.createQRCode.html
*
* @author ==
* @date 2019年5月7日 下午3:34:05
*/
public static void getCodeC(String access_token, String imgFileName) {
String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + access_token;
Map<String, String> param = new HashMap<String, String>();
// param.put("access_token", access_token);
String a = UUID.randomUUID() + "";
String b = UUID.randomUUID() + "";
String c = UUID.randomUUID() + "";
String path = "?a=" + a + "&b=" + b + "&c=" + c + "";
path = "?page/index/index";
param.put("path", path);
try {
HttpUtil.postDataWithJson(url, param, imgFileName);
log.info("下载小程序二维码,保存到文件:" + imgFileName);
} catch (IOException e) {
e.printStackTrace();
log.error(e);
}
}
引用:
import com.alibaba.fastjson.JSONObject;
HttpUtil是自己写的。