Public Account Payment (1): Get the user's openId and public openid
1. Obtain the apikey, appsecret, and merchant ID
Registration of public accounts and merchant accounts
2. Obtain the user's OpenId
1. Set the domain name on the Authorization callback page]
Official explanation: After a user agrees to authorize a public account on the webpage authorization page, the authorization data will be sent to a callback page. The callback page must be under this domain name to ensure security and reliability. The domain name on the callback page does not support IP addresses.
2. the user agrees to authorize
I wrote this url under the menu and asked the user to agree when entering this page.Note: It seems that the authorization is silent, and the user does not know
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
Parameter: appid: Unique Identifier of the Public Account
Redirect_uri: The redirected url, which is the page to jump to after authorization
Scope: Application Authorization scope
Snsapi_base: The authorization page is not displayed. You can only get your openid.
Snsapi_userinfo: The authorization page is displayed. You can obtain the nickname, gender, and location through openid.
State: parameters after redirection
2. After the user agrees, a code will be generated, with a validity period of only 5 minutes.
1 String code = request.getParameter("code")
3. Change code to openId
/*** Constant class ** @ author rory. wu **/public class Constants {// unique credential of a third-party user public static String appid = ""; // The public static String appsecret of the unique credential of a third-party user = ""; // merchant ID public static String mch_id = ""; // get 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 * common tool Class 3 * @ author rory. wu 4 * @ version 1.0 5 * @ since August 05, 2015 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 = httpsRequest (requestUrl, requestMethod, outputS Tr); 14 jsonObject = JSONObject. fromObject (buffer. toString (); 15} catch (ConnectException ce) {16 log. error ("connection Timeout:" + ce. getMessage (); 17} catch (Exception e) {18 log. error ("https request exception:" + e. getMessage (); 19} 20 return jsonObject; 21} 22 23 24 private static StringBuffer httpsRequest (String requestUrl, String requestMethod, String output) 25 throws NoSuchAlgorithmException, NoSuchProviderException, KeyM AnagementException, 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 // read the returned content from the input stream 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 * Get the user's openId and put the code 4 */5 private void setOpenId (String code) returned by session 3 * @ param 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 ("the code is invalid"); 14} else {15 String openId = jsonObject. getString ("openid"); 16 log.info ("openId:" + openId); 17 session. put ("openId", openId); 18} 19}
The format returned by oauth2_url is:
{
"Access_token": "ACCESS_TOKEN ",
"Expires_in": 7200,
"Refresh_token": "REFRESH_TOKEN ",
"Openid": "OPENID", "scope": "SCOPE ",
"Unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
Invalid Code:
{
"Errcode": 40029
, "Errmsg": "invalid code"
}