生成微信小程序码

官方文档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是自己写的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值