1. 程式人生 > >Java獲取小程式帶參二維碼(太陽碼)

Java獲取小程式帶參二維碼(太陽碼)

開發十年,就只剩下這套架構體系了! >>>   

獲取小程式碼
官方API地址 : https://developers.weixin.qq.com/miniprogram/dev/api/qrcode.html

首先使用官方提供的 介面B:適用於需要的碼數量極多的業務場景

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

POST 引數說明(官方)

注意:通過該介面生成的小程式碼,永久有效,數量暫無限制。使用者掃描該碼進入小程式後,開發者需在對應頁面獲取的碼中 scene 欄位的值,再做處理邏輯。使用如下程式碼可以獲取到二維碼中的 scene 欄位的值。除錯階段可以使用開發工具的條件編譯自定義引數 scene=xxxx 進行模擬,開發工具模擬時的 scene 的引數值需要進行 urlencode

以上都是官方文件所示,如使用A介面或C介面可參考官方API,一下開始切入正題java獲取access_token以及介面呼叫

獲取access_token:

public static void GetUrlS() throws ClientProtocolException, IOException  {
        HttpGet httpGet = new HttpGet(
                "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                        + appid + "&secret=" + secret );
        HttpClient httpClient = HttpClients.createDefault();
        HttpResponse res = httpClient.execute(httpGet);
        HttpEntity entity = res.getEntity();
        String result = EntityUtils.toString(entity, "UTF-8");
        JSONObject jsons = JSONObject.fromObject(result);
        String expires_in = jsons.getString("expires_in");
        
        //快取
        if(Integer.parseInt(expires_in)==7200){
            //ok
            String access_token = jsons.getString("access_token");
            System.out.println("access_token:"+access_token);
           
            cont=access_token;
        }else{
            System.out.println("出錯獲取token失敗!");
        }
          
    }
開始調研介面B生成二維碼:
public static void main(String[] args)
        {
            try
            {
                GetUrlS();
                URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token);
                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", "a=1234567890");
                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/Waitforyou/Desktop/abc.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();