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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

网页拉取微信授权

發布時間:2024/3/13 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网页拉取微信授权 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

記錄一下自己寫網頁拉取微信登錄授權的流程

1.首先需要在微信公眾平臺申請一個服務號
微信公眾平臺
2.在微信公眾號請求用戶網頁授權之前,開發者需要先到公眾平臺官網中的“開發 - 接口權限 - 網頁服務 - 網頁帳號 - 網頁授權獲取用戶基本信息”的配置選項中,修改授權回調域名。請注意,這里填寫的是域名(是一個字符串),而不是URL,因此請勿加 http:// 等協議頭;(在這里微信公眾平臺會生成一個校驗文件,把校驗文件放到項目的根目錄下,即可通過審核)
3.通過微信文檔可以得知拉取登陸授權需要四步

首先 根據鏈接用戶可以在微信拉取微信授權 :(appID)https://open.weixin.qq.com/connect/oauth2/authorize?appid=appId&redirect_uri=redirect_uri&response_type=code&scope=snsapi_base&state=123#wechat_redirect

在用戶點擊同意授權之后,微信會回調之前的配置的網頁回調地址;
我在請求微信成功之后返回了一個網頁,用戶掃碼之后出來的就是這個網頁

/** * @author wx * @version 創建時間:2020年4月8日 下午7:25:15 * */ public void callBack(String codeid ) throws IOException { // 獲取到授權標志code(用戶同意微信授權之后產生的code)hLog.info("進入微信回調"+"codeid= "+codeid);String code = getRequest().getParameter("code");// 通過code換取access_tokenString url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + WxAuthUtil.APP_ID + "&secret="+ WxAuthUtil.APP_SECRET + "&code=" + code + "&grant_type=authorization_code"; JSONObject jsonObject = WxAuthUtil.doGetJson(url);String openid = jsonObject.getString("openid");String access_token = jsonObject.getString("access_token");String refresh_token = jsonObject.getString("refresh_token");// 校驗access_token是否失效String checkoutUrl = "https://api.weixin.qq.com/sns/auth?access_token=" + access_token + "&openid=" + openid;JSONObject checkoutInfo = WxAuthUtil.doGetJson(checkoutUrl); // System.out.println("校驗信息-----" + checkoutInfo.toString());if (!"0".equals(checkoutInfo.getString("errcode"))) {// 刷新access_tokenString refreshTokenUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + openid+ "&grant_type=refresh_token&refresh_token=" + refresh_token;JSONObject refreshInfo = WxAuthUtil.doGetJson(checkoutUrl);System.out.println(refreshInfo.toString());access_token = refreshInfo.getString("access_token");}// 使用access_token拉取用戶信息String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid+ "&lang=zh_CN";JSONObject userInfo = WxAuthUtil.doGetJson(infoUrl);String user_id=userInfo.getString("openid");hLog.info("用戶數據-----" + userInfo.toString() + "\n" + "名字-----" + userInfo.getString("nickname") + "\n"+ "頭像-----" + userInfo.getString("headimgurl") + "\n" + "openID-----" + userInfo.getString("openid")+ "\n" + "性別-----" + userInfo.getString("sex"));renderHtml(h);}

這里是兩個工具類

/** * @author wx * @version 創建時間:2020年4月8日 下午7:25:15 * */ public class WxAuthUtil {public static final String APP_ID = "APP_ID";public static final String APP_SECRET = "APP_SECRET ";//請求微信服務public static JSONObject doGetJson(String url) throws IOException {JSONObject jsonObject = null;DefaultHttpClient client = new DefaultHttpClient();final HttpGet httpGet = new HttpGet(url);HttpResponse response = client.execute(httpGet);HttpEntity entity = response.getEntity();if (entity != null) {// 返回結果轉化為JSON對象final String result = EntityUtils.toString(entity, "UTF-8");jsonObject = JSON.parseObject(result);}return jsonObject;} } /** * @author wx * @version 創建時間:2020年4月8日 下午7:23:16 * */ public class WxCheckoutUtil {// 與接口配置信息中的Token要一致private static String token = "Token";/*** 驗證簽名*/public static boolean checkSignature(String signature, String timestamp, String nonce) {String[] arr = new String[] { token, timestamp, nonce };// 將token、timestamp、nonce三個參數進行字典序排序// Arrays.sort(arr);sort(arr);StringBuilder content = new StringBuilder();for (int i = 0; i < arr.length; i++) {content.append(arr[i]);}MessageDigest md = null;String tmpStr = null;try {md = MessageDigest.getInstance("SHA-1");// 將三個參數字符串拼接成一個字符串進行sha1加密byte[] digest = md.digest(content.toString().getBytes());tmpStr = byteToStr(digest);} catch (NoSuchAlgorithmException e) {e.printStackTrace();}content = null;// 將sha1加密后的字符串可與signature對比,標識該請求來源于微信return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;}/*** 將字節數組轉換為十六進制字符串*/private static String byteToStr(byte[] byteArray) {String strDigest = "";for (int i = 0; i < byteArray.length; i++) {strDigest += byteToHexStr(byteArray[i]);}return strDigest;}/*** 將字節轉換為十六進制字符串*/private static String byteToHexStr(byte mByte) {char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };char[] tempArr = new char[2];tempArr[0] = Digit[(mByte >>> 4) & 0X0F];tempArr[1] = Digit[mByte & 0X0F];String s = new String(tempArr);return s;}public static void sort(String a[]) {for (int i = 0; i < a.length - 1; i++) {for (int j = i + 1; j < a.length; j++) {if (a[j].compareTo(a[i]) < 0) {String temp = a[i];a[i] = a[j];a[j] = temp;}}}}}

總結

以上是生活随笔為你收集整理的网页拉取微信授权的全部內容,希望文章能夠幫你解決所遇到的問題。

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