How to Retrieve a WeChat Mini Program User’s OpenID

This article walks through the complete process of obtaining a WeChat Mini Program user's OpenID by using wx.login to get a code, calling the updated auth.code2Session API with appid and appsecret, and handling the server‑side Java implementation that extracts the OpenID and session key.

Coder Trainee
Coder Trainee
Coder Trainee
How to Retrieve a WeChat Mini Program User’s OpenID

The author begins by criticizing the WeChat Open Platform documentation for being outdated and confusing, then moves on to demonstrate how to actually obtain a Mini Program user's OpenID.

Login flow overview

When a Mini Program calls wx.login, it receives a temporary code. The developer’s backend must then send a request to WeChat’s authentication endpoint, providing three parameters: appid (the Mini Program’s unique identifier), appsecret (generated in the Mini Program console), and the code obtained from the client.

The original documentation points to the auth.code2Session path, which currently returns a 404 error. The author supplies the correct, updated URL (e.g.,

https://developers.weixin.qq.com/miniprogram/.../auth.code2Session

) so developers can view the proper API specification.

Server‑side Java implementation

The following Java snippet shows the complete request‑handling logic. It builds the real request URL, sends an HTTPS GET request, parses the JSON response, checks for error codes, and extracts the openid and session_key values.

String realLoginUrl = getRealUrl(code);
logger.info("=================请求微信获用户信息地址:{}", realLoginUrl);
String result = HttpUtils.httpsRequest(realLoginUrl, HttpUtils.GET, null);
JSONObject resJson = JSON.parseObject(result);
String errcode = resJson.getString("errcode");
if (!StringUtils.isEmpty(errcode)) {
    String errmsg = resJson.getString("errmsg");
    logger.info("微信登录异常:{}, {}", errcode, errmsg);
    throw new BussException("微信登录异常");
}
String openid = resJson.getString("openid");
if (StringUtils.isEmpty(openid)) {
    throw new ParamException("无法获取openid信息");
}
String sessionKey = resJson.getString("session_key");
ApiResult apiResult = new ApiResult();
apiResult.getData().put("openid", openid);
apiResult.getData().put("sessionKey", sessionKey);

This code logs the request URL, handles possible error codes returned by WeChat, throws exceptions with Chinese error messages when necessary, and finally packages the OpenID and session key into an ApiResult object.

Result

Running the above flow successfully returns the user's OpenID and session key, completing the authentication process for the Mini Program.

Images in the original article illustrate the login flow diagram and the location of the AppID/AppSecret in the WeChat Mini Program console.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMiniProgramAPIWeChatOpenIDwx.login
Coder Trainee
Written by

Coder Trainee

Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.