1、獲取apikey,appsecret與商戶號json
註冊公衆號、商戶號api
2、獲取用戶的OpenId安全
1.設置【受權回調頁面域名】微信
官方解釋:用戶在網頁受權頁贊成受權給公衆號後,微信會將受權數據傳給一個回調頁面,回調頁面需在此域名下,以確保安全可靠。回調頁面域名不支持IP地址。session
2.用戶贊成受權app
我是把這個url寫在微信菜單下的,當進入這個頁面的時候就讓用戶贊成。注意:好像是靜默受權的,用戶不知道ide
1.url:工具
https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=url&response_type=code&scope=snsapi_userinfo&state=park#wechat_redirect
參數:appid:公衆號的惟一標識url
redirect_uri:重定向的url,就是受權後要跳轉的頁面spa
scope:應用受權做用域
snsapi_base:不彈出受權頁面,直接跳轉,只能獲取用戶openid
snsapi_userinfo:彈出受權頁面,可經過openid拿到暱稱、性別、所在地
state:重定向後帶的參數
2.用戶贊成後會產生一個code,只有5分鐘時間的有效期。
1 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 oauth2_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; }
1 /** 2 * 通用工具類 3 * @author rory.wu 4 * @version 1.0 5 * @since 2015年08月05日 6 */ 7 public class CommonUtil { 8 9 private static Logger log = Logger.getLogger(CommonUtil.class); 10 public static JSONObject httpsRequestToJsonObject(String requestUrl, String requestMethod, String outputStr) { 11 JSONObject jsonObject = null; 12 try { 13 StringBuffer buffer = httpsRequest(requestUrl, requestMethod, outputStr); 14 jsonObject = JSONObject.fromObject(buffer.toString()); 15 } catch (ConnectException ce) { 16 log.error("鏈接超時:"+ce.getMessage()); 17 } catch (Exception e) { 18 log.error("https請求異常:"+e.getMessage()); 19 } 20 return jsonObject; 21 } 22 23 24 private static StringBuffer httpsRequest(String requestUrl, String requestMethod, String output) 25 throws NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException, MalformedURLException, 26 IOException, ProtocolException, UnsupportedEncodingException { 27 28 URL url = new URL(requestUrl); 29 HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); 30 31 connection.setDoOutput(true); 32 connection.setDoInput(true); 33 connection.setUseCaches(false); 34 connection.setRequestMethod(requestMethod); 35 if (null != output) { 36 OutputStream outputStream = connection.getOutputStream(); 37 outputStream.write(output.getBytes("UTF-8")); 38 outputStream.close(); 39 } 40 41 // 從輸入流讀取返回內容 42 InputStream inputStream = connection.getInputStream(); 43 InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); 44 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 45 String str = null; 46 StringBuffer buffer = new StringBuffer(); 47 while ((str = bufferedReader.readLine()) != null) { 48 buffer.append(str); 49 } 50 51 bufferedReader.close(); 52 inputStreamReader.close(); 53 inputStream.close(); 54 inputStream = null; 55 connection.disconnect(); 56 return buffer; 57 }58 }
1 /** 2 * 獲取用戶的openId,並放入session 3 * @param code 微信返回的code 4 */ 5 private void setOpenId(String code) { 6 session.put("code", code); 7 String oauth2_url = Constants.oauth2_url.replace("APPID", Constants.appid).replace("SECRET", Constants.appsecret).replace("CODE", String.valueOf(session.get("code"))); 8 log.info("oauth2_url:"+oauth2_url); 9 JSONObject jsonObject = CommonUtil.httpsRequestToJsonObject(oauth2_url, "POST", null); 10 log.info("jsonObject:"+jsonObject); 11 Object errorCode = jsonObject.get("errcode"); 12 if(errorCode != null) { 13 log.info("code不合法"); 14 }else{ 15 String openId = jsonObject.getString("openid"); 16 log.info("openId:"+openId); 17 session.put("openId", openId); 18 } 19 }
oauth2_url返回的格式是: { "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE", "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" }Code無效時: { "errcode":40029 ,"errmsg":"invalid code" }