成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

最近有個(gè)小程序的項(xiàng)目 需要前端傳code 后端獲取openid 這里是純后端

在這里記錄一下吧

主要代碼:

這里是獲取openid的實(shí)現(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;}//小程序唯一標(biāo)識(shí) (在微信小程序管理后臺(tái)獲取)String wxspAppid = '***********';//小程序的 app secret (在微信小程序管理后臺(tái)獲取)String wxspSecret = '*********************';//授權(quán)(必填)String grant_type = 'authorization_code';//https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code//1、向微信服務(wù)器 使用登錄憑證 code 獲取 session_key 和 openid//請(qǐng)求參數(shù)String params = 'appid=' + wxspAppid + '&secret=' + wxspSecret + '&js_code=' + code + '&grant_type=' + grant_type;//發(fā)送請(qǐng)求String sr = HttpRequest.sendGet('https://api.weixin.qq.com/sns/jscode2session', params);System.out.println('sr========'+sr);//解析相應(yīng)內(nèi)容(轉(zhuǎn)換成json對(duì)象)JSONObject json =JSON.parseObject(sr);System.out.println('json============'+json);//獲取會(huì)話密鑰(session_key)json.get('session_key').toString();String session_key = json.get('session_key').toString();//用戶的唯一標(biāo)識(shí)(openid)String openid = (String) json.get('openid');map.put('session_key',session_key);map.put('openid',openid);return map; }}這里還需要一個(gè)工具類 用來發(fā)送請(qǐng)求的

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方法的請(qǐng)求 * * @param url * 發(fā)送請(qǐng)求的URL * @param param * 請(qǐng)求參數(shù),請(qǐng)求參數(shù)應(yīng)該是 name1=value1&name2=value2 的形式。 * @return URL 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果 */ 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(); // 設(shè)置通用的請(qǐng)求屬性 connection.setRequestProperty('accept', '*/*'); connection.setRequestProperty('connection', 'Keep-Alive'); connection.setRequestProperty('user-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)'); // 建立實(shí)際的連接 connection.connect(); // 獲取所有響應(yīng)頭字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍歷所有的響應(yīng)頭字段 for (String key : map.keySet()) {System.out.println(key + '--->' + map.get(key)); } // 定義 BufferedReader輸入流來讀取URL的響應(yīng) in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) {result += line; }} catch (Exception e) { System.out.println('發(fā)送GET請(qǐng)求出現(xiàn)異常!' + e); e.printStackTrace();}// 使用finally塊來關(guān)閉輸入流finally { try {if (in != null) { in.close();} } catch (Exception e2) {e2.printStackTrace(); }}return result; } /** * 向指定 URL 發(fā)送POST方法的請(qǐng)求 * * @param url * 發(fā)送請(qǐng)求的 URL * @param param * 請(qǐng)求參數(shù),請(qǐng)求參數(shù)應(yīng)該是 name1=value1&name2=value2 的形式。 * @return 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果 */ 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(); // 設(shè)置通用的請(qǐng)求屬性 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請(qǐng)求必須設(shè)置如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流 //out = new PrintWriter(conn.getOutputStream());out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), 'UTF-8')); // 發(fā)送請(qǐng)求參數(shù) out.print(param); // flush輸出流的緩沖 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應(yīng) 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 請(qǐng)求出現(xiàn)異常!'+e); e.printStackTrace();}//使用finally塊來關(guān)閉輸出流、輸入流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;//訂單號(hào)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ù)組轉(zhuǎn)換成字符串 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就會(huì)返回open_id和session_key

中間可能會(huì)有報(bào)錯(cuò) 主要原因在appid和appsecret這,前端需要配置appid才行,總的來說還是很簡(jiǎn)單的 微信支付才是大坑

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

標(biāo)簽: 微信
相關(guān)文章:
主站蜘蛛池模板: 欧美一级录像 | 色偷偷88欧美精品久久久 | 大陆精品自在线拍国语 | 亚洲成人影院在线 | 97在线碰碰观看免费高清 | 精品亚洲欧美高清不卡高清 | 国产成人精品s8p视频 | mm在线视频免费看 | 国产爽的冒白浆的视频高清 | 九九色综合 | 中国一级毛片欧美一级毛片 | 日韩欧美一区二区三区在线观看 | 久久国产精品国产精品 | 欧美日韩中文国产一区二区三区 | 美女被免费视频网站a国产 美女被免费网站视频软件 美女被免费网站在线软件 美女被免费网站在线视频软件 | 色内内免费视频播放 | 免费一级夫妻a | 99久久免费看精品国产一区 | 在线综合亚洲欧美自拍 | 成人国产三级在线播放 | 欧美一级欧美三级在线观看 | 在线观看精品国内福利视频 | 热热涩热热狠狠色香蕉综合 | 久久国产精品久久久 | 亚洲入口 | 99精品欧美一区二区三区美图 | 久草免费手机视频 | 国产精品香蕉一区二区三区 | 91精品国产手机在线版 | 久久综合本色宗合一本色 | 久久精品视频16 | 亚洲好逼 | 99久久精品国产免费 | 亚洲国产欧美日韩第一香蕉 | freese×video性欧美丝袜 | 精品亚洲成a人片在线观看 精品亚洲成a人在线播放 | 免费视频久久看 | 俄罗斯一级成人毛片 | 亚洲国产精品视频 | 9丨精品国产高清自在线看 ⅹxx中国xxx人妖 | 在线免费公开视频 |