前言
java调用微信小程序官方api生成二维码
提示:以下是本篇文章正文内容,下面案例可供参考
1.微信小程序官方文档
代码如下(示例):
String page="小程序路径(必须)";
//获取AccessToken
String accessToken =getAccessToken();
response.setContentType("image/png");
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit"+"?access_token="+accessToken;
Map<String, Object> paraMap = new HashMap();
paraMap.put("page", page); 已经上线后的小程序路径
paraMap.put("width", 155); 二维码宽度和高度
paraMap.put("scene", scene); 要携带的参数
paraMap.put("is_hyaline", true); 是否是透明背景
byte[] result = HttpUtils.doImgPost(url, paraMap);
InputStream is = new ByteArrayInputStream(result);
Random random = new Random();
String name = random.nextInt(10000)+ System.currentTimeMillis() + ".png";
String imgUrl = ossUtil.uploadFile2OSS(is, name, "poster/");
return ResultMap.ok().put("QRurl",imgUrl);
2.获取Token
代码如下(示例):
public static String getAccessToken(){
String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+"appId"+"&secret="+"秘钥";
JSONObject jsonObject = HttpUtils.httpRequest(requestUrl, "GET", null);
return jsonObject.getString("access_token");
}