国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

java 微信小程序code獲取openid的操作

瀏覽:152日期:2022-05-23 14:29:39

最近有個小程序的項目 需要前端傳code 后端獲取openid 這里是純后端

在這里記錄一下吧

主要代碼:

這里是獲取openid的實現(xiàn)類

import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.moszk.frame.basic.utils.HttpRequest;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;@RestControllerpublic class WeiXinSubmitController { @ResponseBody @RequestMapping(value = '/wx/decodeUserInfo', method = RequestMethod.GET) public Map decodeUserInfo(String code) {System.out.println(code);Map map = new HashMap();//登錄憑證不能為空if (code == null || code.length() == 0) { map.put('status', 0); map.put('msg', 'code 不能為空'); return map;}//小程序唯一標識 (在微信小程序管理后臺獲取)String wxspAppid = '***********';//小程序的 app secret (在微信小程序管理后臺獲取)String wxspSecret = '*********************';//授權(必填)String grant_type = 'authorization_code';//https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code//1、向微信服務器 使用登錄憑證 code 獲取 session_key 和 openid//請求參數(shù)String params = 'appid=' + wxspAppid + '&secret=' + wxspSecret + '&js_code=' + code + '&grant_type=' + grant_type;//發(fā)送請求String sr = HttpRequest.sendGet('https://api.weixin.qq.com/sns/jscode2session', params);System.out.println('sr========'+sr);//解析相應內容(轉換成json對象)JSONObject json =JSON.parseObject(sr);System.out.println('json============'+json);//獲取會話密鑰(session_key)json.get('session_key').toString();String session_key = json.get('session_key').toString();//用戶的唯一標識(openid)String openid = (String) json.get('openid');map.put('session_key',session_key);map.put('openid',openid);return map; }}這里還需要一個工具類 用來發(fā)送請求的

import java.io.*;import java.net.URL;import java.net.URLConnection;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.text.SimpleDateFormat;import java.util.*;public class HttpRequest { /** * 向指定URL發(fā)送GET方法的請求 * * @param url * 發(fā)送請求的URL * @param param * 請求參數(shù),請求參數(shù)應該是 name1=value1&name2=value2 的形式。 * @return URL 所代表遠程資源的響應結果 */ public static String sendGet(String url, String param) {String result = '';BufferedReader in = null;try { String urlNameString = url + '?' + param; URL realUrl = new URL(urlNameString); // 打開和URL之間的連接 URLConnection connection = realUrl.openConnection(); // 設置通用的請求屬性 connection.setRequestProperty('accept', '*/*'); connection.setRequestProperty('connection', 'Keep-Alive'); connection.setRequestProperty('user-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)'); // 建立實際的連接 connection.connect(); // 獲取所有響應頭字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍歷所有的響應頭字段 for (String key : map.keySet()) {System.out.println(key + '--->' + map.get(key)); } // 定義 BufferedReader輸入流來讀取URL的響應 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) {result += line; }} catch (Exception e) { System.out.println('發(fā)送GET請求出現(xiàn)異常!' + e); e.printStackTrace();}// 使用finally塊來關閉輸入流finally { try {if (in != null) { in.close();} } catch (Exception e2) {e2.printStackTrace(); }}return result; } /** * 向指定 URL 發(fā)送POST方法的請求 * * @param url * 發(fā)送請求的 URL * @param param * 請求參數(shù),請求參數(shù)應該是 name1=value1&name2=value2 的形式。 * @return 所代表遠程資源的響應結果 */ public static String sendPost(String url, String param, String keyValue) {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('api-key', keyValue); conn.setRequestProperty('user-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)');conn.setRequestProperty('Accept-Charset', 'UTF-8'); // 發(fā)送POST請求必須設置如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 獲取URLConnection對象對應的輸出流 //out = new PrintWriter(conn.getOutputStream());out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), 'UTF-8')); // 發(fā)送請求參數(shù) out.print(param); // flush輸出流的緩沖 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader( new InputStreamReader(conn.getInputStream(),'UTF-8')); String line; while ((line = in.readLine()) != null) { System.out.println(line);result += line; }} catch (Exception e) { System.out.println('發(fā)送 POST 請求出現(xiàn)異常!'+e); e.printStackTrace();}//使用finally塊來關閉輸出流、輸入流finally{ try{if(out!=null){ out.close();}if(in!=null){ in.close();} } catch(IOException ex){ex.printStackTrace(); }}return result; }public static String generateOrderId(){String keyup_prefix=new SimpleDateFormat('yyyyMMddHHmmss').format(new Date());String keyup_append= String.valueOf(new Random().nextInt(899999)+100000);String pay_orderid=keyup_prefix+keyup_append;//訂單號return pay_orderid;}public static String generateTime(){return new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(new Date());} public static String md5(String str) throws NoSuchElementException {try { MessageDigest md = MessageDigest.getInstance('MD5'); md.update(str.getBytes('UTF-8')); byte[] byteDigest = md.digest(); int i; //字符數(shù)組轉換成字符串 StringBuffer buf = new StringBuffer(''); for (int offset = 0; offset < byteDigest.length; offset++) {i = byteDigest[offset];if (i < 0) i += 256;if (i < 16) buf.append('0');buf.append(Integer.toHexString(i)); } // 32位加密 return buf.toString();//toUpperCase // 16位的加密 //return buf.toString().substring(8, 24).toUpperCase();} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { e.printStackTrace(); return null;} }}

如果一切順利的話 傳過來code就會返回open_id和session_key

中間可能會有報錯 主要原因在appid和appsecret這,前端需要配置appid才行,總的來說還是很簡單的 微信支付才是大坑

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: 微信
相關文章:
主站蜘蛛池模板: 国产高清美女一级毛片久久 | 亚洲日韩中文字幕在线播放 | 一本综合久久 | 欧美视频自拍偷拍 | a毛片免费| 欧美高清性色生活片免费观看 | 中文字幕99在线精品视频免费看 | 久久久久久久久久久96av | 九草视频 | 美国一级毛片免费看成人 | 久草中文视频 | 不卡一级aaa全黄毛片 | 亚洲日本在线观看网址 | 岛国大片在线播放免费 | 精品精品国产欧美在线观看 | 免费一级欧美片片线观看 | 亚洲精品欧美精品国产精品 | 香蕉午夜 | 黄色18网站| 一个人看的免费观看日本视频www | 日韩免费一级a毛片在线播放一级 | 精品中文字幕不卡在线视频 | 最新亚洲精品国自产在线 | 中国美女一级看片 | 男女扒开双腿猛进入爽爽视频 | 国产午夜亚洲精品理论片不卡 | 精品久久久中文字幕二区 | 996re免费热在线视频手机 | 亚洲一级理论片 | 久9视频这里只有精品 | 国产欧美在线观看视频 | 亚洲欧洲精品国产二码 | 亚洲区精选网址 | 高清午夜看片a福利在线观看琪琪 | 亚洲欧美日韩精品香蕉 | 国产精品1区 2区 3区 | 精品400部自拍视频在线播放 | 日本成人在线视频网站 | 福利云 | 深夜福利国产福利视频 | 日韩中文字幕精品 |