日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java获取小程序带参二维码(太阳码)

發布時間:2025/3/18 java 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java获取小程序带参二维码(太阳码) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

獲取小程序碼
官方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){//okString 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();}}

轉載于:https://my.oschina.net/waitforyou/blog/3025393

總結

以上是生活随笔為你收集整理的Java获取小程序带参二维码(太阳码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。