How to get the user's openId

Source: Internet
Author: User
Tags openid
This article describes how to obtain the user's openid for public account payment. you need to authorize the callback page domain name and other methods to implement this function. for details, refer to section 1. obtain the apikey, appsecret, and Merchant's number.

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. after the user agrees, a code will be generated, with a validity period of only minutes.

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 oauth_url = "https://api.weixin.qq.com/sns/oauth/access_token? Appid = APPID & secret = SECRET & code = CODE & grant_type = authorization_code ";}
/*** Common tool class ** @ author rory. wu * @ version. * @ since year/month/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. fromObj Ect (buffer. toString ();} catch (ConnectException ce) {log. error ("connection timeout:" + ce. getMessage ();} catch (Exception e) {log. error ("https request exception:" + e. getMessage ();} return jsonObject;} private static StringBuffer httpsRequest (String requestUrl, String requestMethod, String output) throws handle, NoSuchProviderException, KeyManagementException, MalformedURLException, IOException, Protoc OlException, 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 () ;}// read the returned content from the input stream 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 ;}}/*** get the user's openId and put the code returned by session * @ param code */private void setOpenId (String code) {session. put ("code", code); String oauth_url = Constants. oauth_url.replace ("APPID", Constants. appid ). replace ("SECRET", Constants. appsecret ). re Place ("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 ("The code is invalid");} else {String openId = jsonObject. getString ("openid"); log.info ("openId:" + openId); session. put ("openId", openId) ;}} the format returned by oauth_url is: {"access_token": "ACCESS_TOKEN", "expires_in":, "refresh_token": "REFRESH_TOKEN ", "openid": "OPENID", "scope": "SCOPE", "unionid": "o_bmasdasdsad_sgVthMZOPfL"} Code is invalid: {"errcode":, "errmsg ": "invalid code "}

The above content is the public account payment shared by the little editor of the script house. (1) how to get the user's openId? I hope everyone will like it.

For more public account payment (1) how to obtain the user's openId, please follow the PHP Chinese website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.