如何生成微信小程序码(获取微信小程序码)
发表时间:2020-10-23
发布人:葵宇科技
浏览次数:52
前言
在微信小程序的某些业务场景中,需要用户微信扫码后直接进入到小程序的某个页面(有时小程序码还需携带一些参数),在这种场景下,就需要生成小程序码。
流程
1、获取小程序接口调用凭证(accesstoken)
2、获取小程序二维码(获取成功后,微信服务端将返回二进制字节流,因此需要创建一个文件保存图片)
代码案例
1、引入fastjson依赖
<!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
2、Java代码示例
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
/**
* @description 获取小程序二维码
* @author: liyinlong
* @date 2020/10/23 10:11
*/
public class GetMiniQrcode {
public static void main(String[] args) {
String appid = "";
String secret = "";
String token = getAccessToken(appid, secret);
//发送json请求,对象必须封装成json格式
JSONObject params = new JSONObject();
params.put("scene","xiaozhugedeboke");//参数
params.put("page","pages/verifycode/verifycode");
//注意!一定要将对象转成字符串
String param = params.toJSONString();
String file = "e://a.png";
getQrCode("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token, param, file);
}
/**
* @param url 发送请求的URL
* @param param 请求参数
* @return 所代表远程资源的响应结果
* @description 向指定 URL 发送POST方法的请求
* @author: liyinlong
* @date 2020-01-05 21:00
*/
public static String getQrCode(String url, String param,String file) {
System.out.println("\n==============================POST请求开始==============================");
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//设置Content-type 为 application/json
conn.addRequestProperty("Content-type", "application/json");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
InputStream inputStream = conn.getInputStream();
FileOutputStream outputStream = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
outputStream.close();
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println("url:" + url);
System.out.println("POST请求结果:" + result);
System.out.println("==============================POST请求结束==============================\n");
return result;
}
/**
* @author: liyinlong
* @description 获取小程序接口调用凭证
* 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
* 接口调用url https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
* @date 2020/10/23 10:31
* @param appid 小程序appid
* @param appsecert 小程序密钥
* @return 小程序接口调用凭证accesstoken
*/
public static String getAccessToken(String appid,String appsecert) {
System.out.println("\n==============================GET请求开始==============================");
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid
+ "&secret=" + appsecert;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//设置Content-type 为 application/json
conn.addRequestProperty("Content-type", "application/json");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 GET 请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println("url:" + url);
System.out.println("GET请求结果:" + result);
JSONObject res = JSONObject.parseObject(result);
System.out.println("==============================GET请求结束==============================\n");
return res.getString("access_token");
}
}