微信"號付出(一)若何獲得用戶openId。本站提示廣大學習愛好者:(微信"號付出(一)若何獲得用戶openId)文章只能為提供參考,不一定能成為您想要的結果。以下是微信"號付出(一)若何獲得用戶openId正文
1、獲得apikey,appsecret與商戶號
注冊"號、商戶號
2、獲得用戶的OpenId
1.設置【受權回調頁面域名】
官方說明:用戶在網頁受權頁贊成受權給"號後,微信會將受權數據傳給一個回調頁面,回調頁面需在此域名下,以確保平安靠得住。回調頁面域名不支撐IP地址。
2.用戶贊成受權
我是把這個url寫在微信菜單下的,當進入這個頁面的時刻就讓用戶贊成。留意:似乎是靜默受權的,用戶不曉得
1.url:
https://open.weixin.qq.com/connect/oauth/authorize?appid=appid&redirect_uri=url&response_type=code&scope=snsapi_userinfo&state=park#wechat_redirect
參數:appid:"號的獨一標識
redirect_uri:重定向的url,就是受權後要跳轉的頁面
scope:運用受權感化域
snsapi_base:不彈出受權頁面,直接跳轉,只能獲得用戶openid
snsapi_userinfo:彈出受權頁面,可經由過程openid拿到昵稱、性別、地點地
state:重定向後帶的參數
2.用戶贊成後會發生一個code,只要分鐘時光的有用期。
String code = request.getParameter("code")
3.code換openId
/** * 常量類 * @author rory.wu * */ public class Constants { // 第三方用戶獨一憑證 public static String appid = ""; // 第三方用戶獨一憑證密鑰 public static String appsecret = ""; //商戶ID public static String mch_id=""; //獲得openId public static String oauth_url = "https://api.weixin.qq.com/sns/oauth/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; }
/** * 通用對象類 * @author rory.wu * @version . * @since 年代日 */ public class CommonUtil { private static Logger log = Logger.getLogger(CommonUtil.class); public static JSONObject httpsRequestToJsonObject(String requestUrl, String requestMethod, String outputStr) { JSONObject jsonObject = null; try { StringBuffer buffer = httpsRequest(requestUrl, requestMethod, outputStr); jsonObject = JSONObject.fromObject(buffer.toString()); } catch (ConnectException ce) { log.error("銜接超時:"+ce.getMessage()); } catch (Exception e) { log.error("https要求異常:"+e.getMessage()); } return jsonObject; } private static StringBuffer httpsRequest(String requestUrl, String requestMethod, String output) throws NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException, MalformedURLException, IOException, ProtocolException, UnsupportedEncodingException { URL url = new URL(requestUrl); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestMethod(requestMethod); if (null != output) { OutputStream outputStream = connection.getOutputStream(); outputStream.write(output.getBytes("UTF-")); outputStream.close(); } // 從輸出流讀取前往內容 InputStream inputStream = connection.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; StringBuffer buffer = new StringBuffer(); while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } bufferedReader.close(); inputStreamReader.close(); inputStream.close(); inputStream = null; connection.disconnect(); return buffer; } } /** * 獲得用戶的openId,並放入session * @param code 微信前往的code */ private void setOpenId(String code) { session.put("code", code); String oauth_url = Constants.oauth_url.replace("APPID", Constants.appid).replace("SECRET", Constants.appsecret).replace("CODE", String.valueOf(session.get("code"))); log.info("oauth_url:"+oauth_url); JSONObject jsonObject = CommonUtil.httpsRequestToJsonObject(oauth_url, "POST", null); log.info("jsonObject:"+jsonObject); Object errorCode = jsonObject.get("errcode"); if(errorCode != null) { log.info("code不正當"); }else{ String openId = jsonObject.getString("openid"); log.info("openId:"+openId); session.put("openId", openId); } } oauth_url前往的格局是: { "access_token":"ACCESS_TOKEN", "expires_in":, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE", "unionid": "o_bmasdasdsad_sgVthMZOPfL" } Code有效時: { "errcode": ,"errmsg":"invalid code" }
以上內容就是的小編給年夜家分享的微信"號付出(一)若何獲得用戶openId,願望年夜家愛好。