PC logon on WeChat web page scan QR code login write Controller

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn about the recent PC-side QR code login and write a controller, I sorted out the methods and precautions and learned them together.
Let's start with the steps:
1. Registered Open Platform https://open.weixin.qq.com, must be clear open platform and public platform are independent, cannot share.
2. log on to the -- Management Center, and create a website application. Fill in the application, the enterprise also needs to stamp, then set the domain name, and finally pay 300 yuan protection fee. Verification Successful. Obtain the appid and appSecret parameters.
3. Now you can write the logon controller on the web side.
4. website login documentation in https://open.weixin.qq.com/cgi-bin/showdocument? Action = dir_list & t = resource/res_list & verify = 1 & id = open1419425505 & token = 41ab5f757248bbbdcc2aad1a6d52b49fdc19579e & lang = zh_CN.
5. You can use the logon request as a url jump. Https://open.weixin.qq.com/connect/qrconnect? Appid = APPID & redirect_uri = REDIRECT_URI & response_type = code & scope = SCOPE & state = STATE # wechat_redirect
Required parameter description
Appid is the unique ID of an application.
Redirect_uri is the redirection address, which requires UrlEncode
Response_type is the code
Scope is the application Authorization scope. Multiple scopes are separated by commas (,). Currently, only snsapi_login can be entered for Web applications.
State no is used to maintain the state of the request and callback, and the authorization request is taken back to a third party as is. This parameter can be used to prevent csrf attacks (cross-site request forgery attacks). It is recommended that a third party carry this parameter and set it to a simple random number and a session for verification.
Send the appid parameter to the open platform. A QR code is displayed on the Link Page.

6. After scanning the QR code, the user will jump back to your domain name address.
At the same time, the domain name will be followed by the code and state parameters. This code is the key we need
Redirect_uri? Code = CODE & state = STATE
7. On the page or controller redirected to redirect_uri, receive the code value.
Each time the code is generated, it will be destroyed after use, that is, you cannot jump continuously.
8. Request the open access_token interface to obtain the access_token through appid, appSecret, and code value.
{
"Access_token": "ACCESS_TOKEN ",
"Expires_in": 7200,
"Refresh_token": "REFRESH_TOKEN ",
"Openid": "OPENID ",
"Scope": "SCOPE ",
"Unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}

Parameter description
Access_token interface call credential
Expires_inaccess_token API call credential timeout time, in seconds)
Refresh_token user refresh access_token
Openid uniquely identifies an authorized user
Scope user-authorized scopes, separated by commas (,)
This field appears only when the website application has been authorized by the user's userinfo.

Here we will explain that access_token is an interface credential and must be included in the request interface for two hours. Openid is a parameter used to obtain user information, such as username. The unionid is the uid, the unique ID of the user.
9. After obtaining the token and openid, you can obtain user data through the UserInfo interface. Https://api.weixin.qq.com/sns/userinfo? Access_token = ACCESS_TOKEN & openid = OPENID
Correct Json return results:
{
"Openid": "OPENID ",
"Nickname": "NICKNAME ",
"Sex": 1,
"Province": "PROVINCE ",
"City": "CITY ",
"Country": "COUNTRY ",
"Headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0 ",
"Privilege ":[
"PRIVILEGE1 ",
"PRIVILEGE2"
],
"Unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"

}
10. Basically, this is the process.

After the process is complete, describe the problem.
1. If "scope parameter error" occurs when the first step jumps to the interface, it indicates that appid is incorrect or you have not paid the protection fee. Appid with public account is useless
2. when you request a token, you need to use cURL to capture it. CURL crawlers generally fail to capture it. The specific method is not used for further research. It is estimated that the https crawling method is different from http, I wrote an httpGet method to capture the data. The captured content is json data, which requires decode to be converted to an array.
3. to ensure consistency with the mobile phone user, we need to record the openid and unionid. The public account is different from the openid of the open platform, and the unique unionid is the same. However, both openids can obtain user information. In addition to the user table, we recommend that you add a third-party login table with the fields id, uid, type, openid, unionid, and refresh_token. Type 1 is the end, and 2 is the PC end. If a unionid exists, you do not need to create another user information. Create a session or cookie after obtaining the information.
4. to prevent cross-site requests from csrf, I add a random number to the state and store the random number in the session. When the user scans the code for confirmation, if they are inconsistent, they will be considered as cross-site request forgery attacks.

Network Access .class.zip (2.78 KB download: 131 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.