Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Web Page Authorization to obtain user Information in Wechat Development

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to achieve web authorization to obtain user information in the development of Wechat. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Set the domain name of authorization callback: development-> API permissions

Find "Web page authorization to obtain user basic information", click "modify", and enter the authorized callback domain name in the pop-up box. The domain name does not need to be added with http:// (for more information on web page authorization callback domain name, please refer to the documentation of public platform developers).

2. Obtain authorization

In fact, the key to getting user information is to get the user's openid. Bloggers want to enable users to automatically authorize users by clicking the official account menu to open the page, so as to operate the database for the user, so there are two ways:

(1) request authorization page using custom menu

A separate blog post will be written after the custom menu. Here, we briefly describe the authorization through the custom menu, which requires advanced interface permissions and is limited to users who follow the official account to enter the page directly from the menu.

$menu ='{"button": [{"type": "view", "name": "Mall", "url": "https://open.weixin.qq.com/connect/oauth/authorize?appid=xxx&redirect_uri=http://tx.heivr.com/index.php&response_type=code&scope=snsapi_base&state=#wechat_redirect"},{"name":" Express Service", "sub_button": [{"type": "click", "name": "Express delivery" "key": "express"}, {"type": "click", "name": "Express query", "key": "ww"}]},]}'

The view that needs to be authorized directly enter the authorization request address provided by Wechat at url, where:

Appid: enter the AppID in the basic configuration of Wechat public platform

Redirect_uri: enter the address of the page that jumps after the authorization is completed, that is, your own html5 page

State: parameters that jump to the callback page

Response_type: the two scope authorized by web pages. The official documents of Wechat are described as follows:

1. The web authorization initiated by snsapi_base for scope is used to obtain the openid of the user entering the page, and it is silently authorized and automatically redirected to the callback page. What the user perceives is to go directly to the callback page (often the business page).

2. The web page authorization initiated by snsapi_userinfo for scope is used to obtain the basic information of the user. However, this kind of authorization requires the user to agree manually, and because the user has agreed, the basic information of the user can be obtained without attention after authorization.

According to this method, you can click "Mall" to receive the returned openid, and then proceed to obtain the user information in the next step.

(2) use JS to automatically request authorization page

This method is relatively clumsy and the steps are slightly complicated, but there is no simplified method to solve the requirements, and because of the jump of the page, the time to visit the page will increase in most cases, but compared with the previous method, this method can obtain the basic information of non-concerned users. Some programs may involve page sharing, the program does not force attention, but other users directly enter the page through sharing also need to record user information, you can consider this method. (the code related to Wechat development is encapsulated by bloggers into tool class calls. Here, the parts used are posted first, and all of them will be posted with download links later.)

The idea of this method is as follows: js requests link to obtain code-- > exchange code for openid-> get basic user information.

a. Edit configuration

In order to facilitate the writing of some Wechat parameters into a single class, it is convenient to modify, add and call

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report