1. Obtain the apikey, appsecret and merchant number
Register the public account and merchant number
2. Obtain the user’s OpenId
1. Set the [authorization callback page domain name]
Official explanation: After the user agrees to authorize the official account on the web authorization page, WeChat will pass the authorization data to a callback page, and the callback page needs to be here domain name to ensure safety and reliability. The callback page domain name does not support IP addresses.
https://open.weixin.qq.com/connect/oauth/authorize?appid=appid&redirect_uri=url&response_type =code&scope=snsapi_userinfo&state=park#wechat_redirect
String code = request.getParameter("code")3. Code for 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("連接超時(shí):"+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(); } // 從輸入流讀取返回內(nèi)容 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無效時(shí): { "errcode": ,"errmsg":"invalid code" }The above content is from Script House The editor will share with you how to obtain user openId for WeChat public account payment (1). I hope you like it. For more WeChat public account payment (1) How to obtain user openId related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
