Joke Collection Website - Blessing messages - Wechat official account to obtain user information.

Wechat official account to obtain user information.

When developing the official WeChat account, one of the functions we often use is to obtain the user's information. But what are the ways to obtain user information? I currently use two.

One is to obtain user information through webpage authorization. One is to obtain the basic information of the user according to the user OpenID after the message interaction between the user and the official WeChat account or after the event after attention is pushed.

Please pay special attention to two points:

The first point is that they have different addresses when requesting user information from WeChat.

Interface address of "Access to User Basic Information Interface" for webpage authorization request:

/sns/userinfo? Access token = access token & OpenID = OpenID & lang = zh _ cn

Address of "Access User Basic Information Interface" in user management interface:

/cgi-bin/user/info? Access token = access token & OpenID = OpenID & lang = zh _ cn

The second point is that the webpage authorization access_token used by "webpage authorization" to obtain user information is different from ordinary access_token, which can be understood as an access_token between users, service numbers and WeChat. Access_token used in "Interface for Obtaining User Basic Information" in user management interface is the only interface calling voucher for official WeChat account, and it is necessary for official WeChat account to use access_token when calling all interfaces.

The first type: Web pages authorize access to user information.

Wechat document address: Web page authorization

Explain the difference between the authorization scope of two web pages.

The webpage authorization initiated by 1.snsapi_base is used to obtain the openid of the user who enters the page, and it is silent authorization and automatically jumps to the callback page. What users perceive is that they go directly to the callback page (usually the business page).

2. The webpage authorized by 2.snsapi_userinfo is used to obtain the basic information of users. However, this authorization requires the user's manual consent, and because the user has already agreed, you can get the basic information of the user after authorization without paying attention.

The webpage authorization process is divided into four steps:

1. Guide the user to enter the authorization page to agree to authorization and get the code.

2. Web page authorizes the exchange code of access_token (different from access_token in basic support).

3. If necessary, the developer can refresh the webpage authorization access_token to avoid expiration.

4. access_token and openid are authorized through the webpage to obtain the basic information of users.

Web page authorization request WeChat address: /sns/userinfo? Access token = access token & OpenID = OpenID & lang = zh _ cn

Requested parameters:

We can obtain the following user information through webpage authorization:

The second way to get user information is to call the "Get User Basic Information Interface" in the user management interface.

Document address: user management to obtain user information

Request parameters:

User information obtained: