How to implement webpage authorization on WeChat official account

Source: Internet
Author: User
To ensure that the public account has the permission to authorize the scope (scope parameter) (after the service number obtains the advanced interface, it has the snsapi_base and snsapi_userinfo in the scope parameter by default), the supervisor is guided to open the following page: develop Official documents based on the public account:

To obtain user information, follow these steps:

1 Step 1: the user agrees to the authorization and obtains the code
2 Step 2: exchange code for webpage authorization access_token
3 Step 3: Refresh access_token (if needed)
4. Step 4: pull user information (the scope must be snsapi_userinfo)

1 Get code

After obtaining the advanced http://www.php.cn/code/11829.html "target =" _ blank "> interface, by default, snsapi_base and snsapi_userinfo in the scope parameter are available ).

If the message "This link cannot be accessed" is displayed, check whether the parameter is entered incorrectly and whether the permission for the scope parameter is authorized.

Note: Due to the high security level of the authorization operation, the authorization link will undergo regular and strong match verification when initiating the authorization request. if the parameter order of the link is incorrect, the authorization page cannot be accessed normally.

Where:

AppID-unique ID of the public account
REDIRECT_URI-jump url
SCOPE-the value is snsapi_base (the authorization page is not displayed, and you can only get the user's openid) or snsapi_userinfo (the authorization page is displayed. you can get the nickname, gender, and location through openid. In addition, users can obtain information even if they do not pay attention to it)
STATE-developers can customize parameter values for entering a-zA-Z0-9

2. exchange code for webpage authorization access_token

If the user agrees to the authorization, the page will jump to redirect_uri /? Code = CODE & state = STATE.
The state parameter is passed as is.

Implementation code:

$ Code = I ('Get. code'); if (empty ($ code) {// todo non-access exit ('');} Else {// authorized Operation}

Here we can get the code as the subsequent access_token.

After obtaining the code, request the following link to obtain the access_token:

Appid-unique ID of the public account
Secret-key
Code-the code returned above
Grant_type-value: authorization_code

Implementation code:

$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . C('wechat.AppID') . '&secret=' . C('wechat.AppSecret');$str = file_get_contents($url);$str = json_decode($str, true);$access_token = $str['access_token'];

Access_token can be cached to avoid frequent access.
Implementation code. take the TP framework as an example:

$access_token = S('access_token');if (empty($access_token)) {  $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . C('wechat.AppID') . '&secret=' . C('wechat.AppSecret');  $str = file_get_contents($url);  $str = json_decode($str, true);  $access_token = $str['access_token'];  S('access_token', $access_token, 3600);}

After obtaining the access_token, the openid (unique user ID) will also be returned. the official document explains: Unique user id. Note that when you do not pay attention to the public ID, when a user accesses a webpage with a public account, a unique OpenID is generated for the user and the public account.

Openid uniquely identifies a user. if the user does not log on for the first time, you can query whether the database has a user bound to this openid after obtaining the openid. then, you do not need to obtain user data again, set the session for the database user_id directly obtained and log on to the database directly.

3. skip the third step. you only need to obtain the access_token in the required time.

4. pull user information (the scope must be snsapi_userinfo)

If the database does not bind a user with this number, it is equivalent to the first time the user accesses and logs on to the database. The fourth step is used to obtain the user information (with the user authorization, the webpage authorization scope is snsapi_userinfo, then, the developer can use access_token and openid to pull user information.) Then, create a user in the background and bind

Trusted user (via openid)

Request method

Http: GET (use https protocol)

Access_token-the access_token obtained above
Openid-unique public ID

$ Url = "https://api.weixin.qq.com/cgi-bin/user/info? Access_token = $ access_token & openid = $ openid & lang = zh_CN "; $ str = file_get_contents ($ url); $ str = json_decode ($ str, true ); if (empty ($ str ['errcode']) {// Create a user and bind the openid} else {// error handling}

The above is the detailed content of the implementation steps for obtaining public account web page authorization. For more information, see other related articles on php Chinese network!

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.