java生成小程序二维码-自定义参数

第一次写博客,有点紧张,不足之处,请大佬们轻喷,代码也不是我自己写的,我也是这里copy一点那里copy一点,然后总结出来的,前面可能废话比较多,因为我也是小程序的初学者,看官网的API看了好久都没看懂,所以把我理解的记下来了,希望这篇对大家有帮助

微信官网小程序二维码API:https://developers.weixin.qq.com/miniprogram/dev/api/qrcode.html

为满足不同需求和场景,这里提供了三个接口,开发者可挑选适合自己的接口。

A接口,生成小程序码,可接受path参数较长,生成个数受限。

B接口,生成小程序码,可接受页面参数较短,生成个数不受限。

C接口,生成二维码,可接受path参数较长,生成个数受限。

接口A:https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN

接口B:https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

该接口主要用于获取二维码(不带参数)

接口C:https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN

注意事项

上面是官网的一些资料,接下来就通过Java后台来获取到带参数的小程序码

一、获取access_token

1.https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183进入官方文档,有详细介绍

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

(1)里面给了一个https的请求方式,这个是干嘛用的呢?

注意里面有三个参数”appid”和”secret”,还有一个就不用管,我们这个固定写法就好了

appid:小程序id

secret:小程序密钥

知道参数是干嘛的了,那下一步怎么用呢,有两种方式,一种是直接在地址栏上面

好了,想要的access_token获取到了,直接Ctrl+C Ctrl+V就好了

等等,这样做是不是好像不太符合程序员的做法?

是的,不安全,直接把小程序id和密钥都暴露出来了,那怎么办呢?

我们可以用java后台来,废话不多说,直接看码

/**
     * 用于获取access_token
     * @param params
     * @param APIKEY 小程序id
     * @param SECRETKEY    小程序密钥
     * @return  access_token
     * @throws Exception
     */
    public static String postToken() throws Exception {
        String APIKEY = "";//小程序id
        String SECRETKEY = "";//小程序密钥
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APIKEY+"&secret="+SECRETKEY;
        URL url = new URL(requestUrl);
        // 打开和URL之间的连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        // 设置通用的请求属性
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);
 
        // 得到请求的输出流对象
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.writeBytes("");
        out.flush();
        out.close();
 
        // 建立实际的连接
        connection.connect();
        // 定义 BufferedReader输入流来读取URL的响应
        BufferedReader in = null;
        if (requestUrl.contains("nlp"))
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
        else
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        String result = "";
        String getLine;
        while ((getLine = in.readLine()) != null) {
            result += getLine;
        }
        in.close();
        JSONObject jsonObject = JSON.parseObject(result);
        String accesstoken=jsonObject.getString("access_token");
        return accesstoken;
    }
 

好了,这个方法就直接返回access_token,是不是很简单啊

Accesstoken获取完了,下一步我们就可以调用之前说的接口来生成我们带参数的小程序码了

二、生成小程序码

看到这里是不是大佬们都知道下一步该怎么做了?

你们是这样想的吗?嗯哼?看到什么了?errmsg

那应该怎么办呢?没事,不要急,老规矩,废话不多说,直接上码

/**
     * 生成带参小程序二维码
     * @param sceneStr    参数
     * @param accessToken    token
     */
     public static void getminiqrQr(String sceneStr, String accessToken) {
         try
            {
                URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");// 提交模式
                // conn.setConnectTimeout(10000);//连接超时 单位毫秒
                // conn.setReadTimeout(2000);//读取超时 单位毫秒
                // 发送POST请求必须设置如下两行
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                // 获取URLConnection对象对应的输出流
                PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
                // 发送请求参数
                JSONObject paramJson = new JSONObject();
                paramJson.put("scene", sceneStr);
                paramJson.put("page", "pages/index/index");
                paramJson.put("width", 430);
                paramJson.put("auto_color", true);
                /**
                 * line_color生效
                 * paramJson.put("auto_color", false);
                 * JSONObject lineColor = new JSONObject();
                 * lineColor.put("r", 0);
                 * lineColor.put("g", 0);
                 * lineColor.put("b", 0);
                 * paramJson.put("line_color", lineColor);
                 * */
 
                printWriter.write(paramJson.toString());
                // flush输出流的缓冲
                printWriter.flush();
                //开始获取数据
                BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
                OutputStream os = new FileOutputStream(new File("C:/Users/Administrator/Desktop/1.png"));
                int len;
                byte[] arr = new byte[1024];
                while ((len = bis.read(arr)) != -1)
                {
                    os.write(arr, 0, len);
                    os.flush();
                }
                os.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
 

基本上就已经完成了!测试一下,上码吧

                            然后你的桌面就会生成一个带参数的小程序码了

                                                                                    Over!

大佬们反映的几个问题,我这边归纳一下:

1.为什么生成不了二维码?

 答:检查一下小程序ID和密钥是否填写正确;确认小程序有没有上线,如果没有上线也是生成不了二维码的。

2.为什么生成了二维码后小程序取不到参数?

 答:确认一下前端有没有正确获取小程序码参数的代码,前端的代码我就不贴了。

帮助过几个人远程都是可以用的,还有不懂的同学可以下面留言。


————————————————
版权声明:本文为CSDN博主「仙_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36466653/article/details/80106903

发布了168 篇原创文章 · 获赞 16 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/ajax_yan/article/details/102779576