


Introduction to WeChat login for website applications developed by WeChat open platform
Mar 09, 2017 pm 03:06 PMIn this WeChat public platform development tutorial, we will introduce how to use the WeChat open platform interface to implement the WeChat QR code login function.
Preparation
The website application WeChat login is a WeChat OAuth2.0 authorized login system built based on the OAuth2.0 protocol standard.
Before performing WeChat OAuth2. Before performing WeChat OAuth2.0 authorized login and access, register a developer account on the WeChat open platform, have an approved website application, and obtain the corresponding AppID and AppSecret. After applying for WeChat login and passing the review, you can start the access process.
Authorization process description
WeChat OAuth2.0 authorized login allows WeChat users to use WeChat identities to securely log in to third-party applications or websites. After the WeChat user has authorized login After accessing the third-party application of WeChat OAuth2.0, the third party can obtain the user's interface call credential (access_token). Through the access_token, the WeChat open platform authorization relationship interface can be called, thereby obtaining the basic open information of WeChat users and helping users. Implement basic open functions, etc.
WeChat OAuth2.0 authorized login currently supports authorization_code mode, which is suitable for application authorization with server side. The overall process of this model is:
1. A third party initiates a WeChat authorized login request. After the WeChat user allows authorization of the third-party application, WeChat will launch the application or restart it. Direct to the third-party website and bring the authorization temporary ticket code parameter;
2. Add AppID and AppSecret through the code parameter, and exchange for access_token through the API;
3. Call the interface through access_token to obtain the user's basic data resources or help the user implement basic operations.
Get access_token sequence diagram:
Step 1: Request CODE
Log in to Fangbei Studio WeChat login website application
http://weixin.fangbei.org/login.php
After opening, the application will generate state parameters, jump to the following link: (please log in before Note that the corresponding web page authorization scope (scope=snsapi_login) has been obtained)
https://open.weixin.qq.com/connect/qrconnect?appid=wxed782be999f86e0e&redirect_uri=http%3A%2F%2Fweixin.fangbei.org%2Flogin.php&response_type=code&scope=snsapi_login&state=123#wechat_redirect
If it prompts "The link cannot be accessed", please check whether the parameters are filled in Errors, such as the domain name of redirect_uri is inconsistent with the authorized domain name filled in during review or the scope is not snsapi_login.
Parameter Description
Parameter | Is it necessary | Description |
---|---|---|
appid | is the | application unique identifier |
redirect_uri | is | Redirect address needs to be UrlEncode |
response_type | is | fill in code |
scope | is the | application authorization scope, with multiple scopes separated by commas (,), web applications currently only need to fill in snsapi_login |
state | No | Used to maintain the status of the request and callback, and bring it back to the third party intact after the request is authorized. This parameter can be used to prevent CSRF attacks (cross-site request forgery attacks). It is recommended that third parties bring this parameter. It can be set to a simple random number plus session for verification |
返回說明
此時,PC網站上顯示如下二維碼
?
用戶允許授權后,將會重定向到redirect_uri的網址上,并且?guī)蟘ode和state參數(shù)
http://weixin.fangbei.org/login.php?code=0317a2c31ccd5eadf1a7a8fffd4a7dbf&state=123
為了滿足網站更定制化的需求,我們還提供了第二種獲取code的方式,支持網站將微信登錄二維碼內嵌到自己頁面中,用戶使用微信掃碼授權后通過JS將code返回給網站。
JS微信登錄主要用途:網站希望用戶在網站內就能完成登錄,無需跳轉到微信域下登錄后再返回,提升微信登錄的流暢性與成功率。 網站內嵌二維碼微信登錄JS實現(xiàn)辦法:
步驟1:在頁面中先引入如下JS文件(支持https):
<script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
步驟2:在需要使用微信登錄的地方實例以下JS對象:
????????<script> ????????????var?obj?=?new?WxLogin({ ??????????????id:?"login_container", ??????????????appid:?"wxed782be999f86e0e", ??????????????scope:?"snsapi_login", ??????????????redirect_uri:?encodeURIComponent("http://"?+?window.location.host?+?"/login.php"), ??????????????state:?Math.ceil(Math.random()*1000), ??????????????style:?"black", ??????????????href:?""});????????</script>
參數(shù)說明
參數(shù) | 是否必須 | 說明 |
---|---|---|
id | 是 | 第三方頁面顯示二維碼的容器id |
appid | 是 | 應用唯一標識,在微信開放平臺提交應用審核通過后獲得 |
scope | 是 | 應用授權作用域,擁有多個作用域用逗號(,)分隔,網頁應用目前僅填寫snsapi_login即可 |
redirect_uri | 是 | 重定向地址,需要進行UrlEncode |
state | 否 | 用于保持請求和回調的狀態(tài),授權請求后原樣帶回給第三方。該參數(shù)可用于防止csrf攻擊(跨站請求偽造攻擊),建議第三方帶上該參數(shù),可設置為簡單的隨機數(shù)加session進行校驗 |
style | 否 | 提供"black"、"white"可選,默認為黑色文字描述。詳見文檔底部FAQ |
href | 否 | 自定義樣式鏈接,第三方可根據(jù)實際需求覆蓋默認樣式。詳見文檔底部FAQ |
完整代碼如下
???? ???????? ???? ???? ???????? ????????<script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script> ????????<script> ????????????var?obj?=?new?WxLogin({ ??????????????id:?"login_container", ??????????????appid:?"wxed782be999f86e0e", ??????????????scope:?"snsapi_login", ??????????????redirect_uri:?encodeURIComponent("http://"?+?window.location.host?+?"/login.php"), ??????????????state:?Math.ceil(Math.random()*1000), ??????????????style:?"black", ??????????????href:?""});????????</script> ????
頁面顯示效果如下?
?
第二步:通過code獲取access_token
通過code獲取access_token
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
參數(shù)說明
參數(shù) | 是否必須 | 說明 |
---|---|---|
appid | 是 | 應用唯一標識,在微信開放平臺提交應用審核通過后獲得 |
secret | 是 | 應用密鑰AppSecret,在微信開放平臺提交應用審核通過后獲得 |
code | 是 | 填寫第一步獲取的code參數(shù) |
grant_type | 是 | 填authorization_code |
返回說明
正確的返回:
{????"access_token":?"OezXcEiiBSKSxW0eoylIeFy2HFC4Bxv9JvC0Sgj4Px4_8TX1ci3jF_QP_6sWjvx2rCAUjXEP1_9edZdJLf3MIwii2N8cnTooDfx7nYpFRmOSZyq4gb2FNdWJr__KUqPtcfVUvg6XBTucZZ4zH6v8VQ",????"expires_in":?7200,????"refresh_token":?"OezXcEiiBSKSxW0eoylIeFy2HFC4Bxv9JvC0Sgj4Px4_8TX1ci3jF_QP_6sWjvx2lW60INlf6AK1q21rW7mJyc5yG3GZ9p1psANOKTi2EZUQXA6CnwSXxDQlJ3421tEOvCWIrJhkA8oTqjsLKYG-yg",????"openid":?"oJekJs2faTQ47FGjDOEIyOPMN97s",????"scope":?"snsapi_login",????"unionid":?"o4wcnw02YjFUYglZxV0LwcBkVF6Y"}
參數(shù)說明
參數(shù) | 說明 |
---|---|
access_token | 接口調用憑證 |
expires_in | access_token接口調用憑證超時時間,單位(秒) |
refresh_token | 用戶刷新access_token |
openid | 授權用戶唯一標識 |
scope | 用戶授權的作用域,使用逗號(,)分隔 |
?unionid | 當且僅當該網站應用已獲得該用戶的userinfo授權時,才會出現(xiàn)該字段。 |
錯誤返回樣例:
{"errcode":40029,"errmsg":"invalid code"}
刷新access_token有效期
access_token是調用授權關系接口的調用憑證,由于access_token有效期(目前為2個小時)較短,當access_token超時后,可以使用refresh_token進行刷新,access_token刷新結果有兩種:
1. 若access_token已超時,那么進行refresh_token會獲取一個新的access_token,新的超時時間;
2. 若access_token未超時,那么進行refresh_token不會改變access_token,但超時時間會刷新,相當于續(xù)期access_token。
refresh_token擁有較長的有效期(30天),當refresh_token失效的后,需要用戶重新授權。
請求方法
獲取第一步的code后,請求以下鏈接進行refresh_token:
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
參數(shù)說明
參數(shù) | 是否必須 | 說明 |
---|---|---|
appid | 是 | 應用唯一標識 |
grant_type | 是 | 填refresh_token |
refresh_token | 是 | 填寫通過access_token獲取到的refresh_token參數(shù) |
返回說明
正確的返回:
{?<br>"access_token":"ACCESS_TOKEN",?<br>"expires_in":7200,?<br>"refresh_token":"REFRESH_TOKEN",?<br>"openid":"OPENID",?<br>"scope":"SCOPE"?<br>}
參數(shù) | 說明 |
---|---|
access_token | 接口調用憑證 |
expires_in | access_token接口調用憑證超時時間,單位(秒) |
refresh_token | 用戶刷新access_token |
openid | 授權用戶唯一標識 |
scope | 用戶授權的作用域,使用逗號(,)分隔 |
Error return example:
##{"errcode":40030,"errmsg":"invalid refresh_token"}
- 1. The access_token is valid and has not expired;
- 2. The WeChat user has authorized the corresponding interface scope (scope) of the third-party application account.
Interface | Interface Description | |
---|---|---|
/sns/oauth2/access_token | Exchange access_token, refresh_token and authorized scope through code | |
Refresh or renew access_token usage | ||
Check access_token validity | ||
/sns/userinfo | Get user personal information |
The above is the detailed content of Introduction to WeChat login for website applications developed by WeChat open platform. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is an open source scripting language that is widely used in web development and server-side programming, especially in WeChat development. Today, more and more companies and developers are starting to use PHP for WeChat development because it has become a truly easy-to-learn and easy-to-use development language. In WeChat development, message encryption and decryption are a very important issue because they involve data security. For messages without encryption and decryption methods, hackers can easily obtain the data, posing a threat to users.

In the development of WeChat public accounts, the voting function is often used. The voting function is a great way for users to quickly participate in interactions, and it is also an important tool for holding events and surveying opinions. This article will introduce you how to use PHP to implement WeChat voting function. Obtain the authorization of the WeChat official account. First, you need to obtain the authorization of the WeChat official account. On the WeChat public platform, you need to configure the API address of the WeChat public account, the official account, and the token corresponding to the public account. In the process of our development using PHP language, we need to use the PH officially provided by WeChat

With the popularity of WeChat, more and more companies are beginning to use it as a marketing tool. The WeChat group messaging function is one of the important means for enterprises to conduct WeChat marketing. However, if you only rely on manual sending, it is an extremely time-consuming and laborious task for marketers. Therefore, it is particularly important to develop a WeChat mass messaging tool. This article will introduce how to use PHP to develop WeChat mass messaging tools. 1. Preparation work To develop WeChat mass messaging tools, we need to master the following technical points: Basic knowledge of PHP WeChat public platform development Development tools: Sub

WeChat is currently one of the social platforms with the largest user base in the world. With the popularity of mobile Internet, more and more companies are beginning to realize the importance of WeChat marketing. When conducting WeChat marketing, customer service is a crucial part. In order to better manage the customer service chat window, we can use PHP language for WeChat development. 1. Introduction to PHP WeChat development PHP is an open source server-side scripting language that is widely used in the field of Web development. Combined with the development interface provided by WeChat public platform, we can use PHP language to conduct WeChat

In the development of WeChat public accounts, user tag management is a very important function, which allows developers to better understand and manage their users. This article will introduce how to use PHP to implement the WeChat user tag management function. 1. Obtain the openid of the WeChat user. Before using the WeChat user tag management function, we first need to obtain the user's openid. In the development of WeChat public accounts, it is a common practice to obtain openid through user authorization. After the user authorization is completed, we can obtain the user through the following code

As WeChat becomes an increasingly important communication tool in people's lives, its agile messaging function is quickly favored by a large number of enterprises and individuals. For enterprises, developing WeChat into a marketing platform has become a trend, and the importance of WeChat development has gradually become more prominent. Among them, the group sending function is even more widely used. So, as a PHP programmer, how to implement group message sending records? The following will give you a brief introduction. 1. Understand the development knowledge related to WeChat public accounts. Before understanding how to implement group message sending records, I

How to use PHP to develop WeChat public accounts WeChat public accounts have become an important channel for promotion and interaction for many companies, and PHP, as a commonly used Web language, can also be used to develop WeChat public accounts. This article will introduce the specific steps to use PHP to develop WeChat public accounts. Step 1: Obtain the developer account of the WeChat official account. Before starting the development of the WeChat official account, you need to apply for a developer account of the WeChat official account. For the specific registration process, please refer to the official website of WeChat public platform

With the development of the Internet and mobile smart devices, WeChat has become an indispensable part of the social and marketing fields. In this increasingly digital era, how to use PHP for WeChat development has become the focus of many developers. This article mainly introduces the relevant knowledge points on how to use PHP for WeChat development, as well as some of the tips and precautions. 1. Development environment preparation Before developing WeChat, you first need to prepare the corresponding development environment. Specifically, you need to install the PHP operating environment and the WeChat public platform
