乐闻世界logo
搜索文章和话题

How to find my OpenId of Wechat personal account which is following a Wechat Service Account

1个答案

1

Obtaining the OpenID of users in WeChat Mini Programs is a common yet important feature, primarily used to uniquely identify a user. Below are the steps to obtain the OpenID:

  1. Register the Mini Program Account: First, developers must register a Mini Program on the WeChat Public Platform (mp.weixin.qq.com) to obtain the Mini Program's AppID and AppSecret.

  2. User Login to the Mini Program: When a user opens the Mini Program, it calls the wx.login() method, which triggers the user login process. WeChat then returns a code.

  3. Send Code to Developer Server: The Mini Program sends this code to the developer's server.

  4. Server Requests WeChat API to Obtain OpenID: The developer's server uses the received code along with the Mini Program's AppID and AppSecret to send a request to the WeChat server. Specifically, the following API can be used:

    shell
    https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=CODE&grant_type=authorization_code

    This request returns a JSON response containing the OpenID and session_key.

  5. Process and Store OpenID: The developer's server parses this JSON response to obtain the OpenID and can store it as needed for subsequent user identification and data processing.

Example

Suppose I have a Mini Program for online food ordering that needs to identify and record user orders. After the user opens the Mini Program and grants login authorization, the Mini Program side obtains the code via wx.login(), then sends the code to my server. My server then calls the WeChat jscode2session API to obtain the user's OpenID. Subsequently, I can associate this OpenID with the user's order information to manage orders.

This process ensures a seamless user experience and secure data processing, while adhering to WeChat's development guidelines.

2024年6月29日 12:07 回复

你的答案