[TOC] #### 1. 前言 ---- [網(wǎng)頁(yè)授權(quán)官方文檔](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html "網(wǎng)頁(yè)授權(quán)官方文檔") #### 2. 第一步:用戶同意授權(quán),獲取 code --- 引導(dǎo)用戶打開(kāi)授權(quán)頁(yè)面 下面是微信官方給出的授權(quán)地址,以下 url 中大寫的參數(shù)值代表的動(dòng)態(tài)參數(shù),需要開(kāi)發(fā)者去傳參,小寫的參數(shù)值代表固定值,無(wú)需動(dòng)態(tài)修改 appid:公眾號(hào) appid redirect_uri: 用戶同意授權(quán)后的回調(diào)地址 scope:授權(quán)作用域,可取值:snsapi_base、snsapi_userinfo snsapi_base 不彈出授權(quán)頁(yè)面,只能獲取到用戶openid snsapi_userinfo 彈出授權(quán)頁(yè)面,可獲取到用戶openid、昵稱、頭像等信息 ``` https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect ``` 用戶同意授權(quán)后將跳轉(zhuǎn)到授權(quán)地址中的 redirect_uri 回調(diào)地址,并且攜帶 code 和 state 兩個(gè)參數(shù) ``` redirect_uri/?code=CODE&state=STATE ``` 在 tp6.0 中的使用示例 ``` public function index() { // 公眾號(hào)appid $appid = 'wx0a48233ca8de759e'; // 回調(diào)地址 $redirect_uri = url('grant', [], true, true); // 對(duì)回調(diào)地址進(jìn)行url編碼 $redirect_uri = urlencode($redirect_uri); // 授權(quán)作用域 $scope = 'snsapi_base、snsapi_userinfo'; // snsapi_base、snsapi_userinfo // 授權(quán)頁(yè)面 $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope={$scope}&state=STATE#wechat_redirect"; // 跳轉(zhuǎn)到授權(quán)頁(yè)面讓用戶授權(quán) return redirect($url); } ``` #### 3. 第二步:通過(guò)code換取網(wǎng)頁(yè)授權(quán) access_token (網(wǎng)頁(yè)授權(quán)接口調(diào)用憑證) --- snsapi_base、snsapi_userinfo 返回的內(nèi)容格式一樣,只是 scope 為各自相應(yīng)的值 ```json { "access_token":"46_F7gxRgt-dgY7SWa43sX8xxxx", "expires_in":7200, "refresh_token":"46_a-XaTkDiL5e47vEYVa3emUjMxxx", "openid":"ozlgE6DGcfpHE1Qz69U9xKQtsRkw", "scope":"snsapi_userinfo" } ``` 特別注意:snsapi_base 式的網(wǎng)頁(yè)授權(quán)流程到此為止 #### 4. 第三步:刷新 access_token (網(wǎng)頁(yè)授權(quán)接口調(diào)用憑證) --- #### 5. 第四步:拉取用戶信息 --- 根據(jù)第二步或第三步獲取到的 `access_token` 和 `openid` 獲取用戶信息 ``` $openid = $res['openid']; $access_token = $res['access_token']; $api = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN"; $info = $this->http_curl($api); halt($info); ``` 返回示例 ``` ^ array:9 [▼ "openid" => "ozlgE6DGcfpHE1Qz69U9xKQtsRkw" "nickname" => "liang" "sex" => 1 "language" => "zh_CN" "city" => "開(kāi)封" "province" => "河南" "country" => "中國(guó)" "headimgurl" => "https://thirdwx.qlogo.cn/mmopen/vi_32/L3D1cS26ogySia7UNlTScztBicIWYFhSzzUnXalicpaYCJAia4oewvGgVw0qCZ58t8eqcb2FsBYTN2wiaiasiabDkHfWg/132" "privilege" => [] ] ``` #### 6. 網(wǎng)頁(yè)授權(quán)常見(jiàn)錯(cuò)誤 ---- **一、提示微信客戶端打開(kāi)鏈接** 因?yàn)榫W(wǎng)頁(yè)授權(quán)是要獲取微信用戶的信息,所以必須在微信瀏覽器中打開(kāi)授權(quán)頁(yè)面(微信客戶端:`手機(jī)端` 和 `PC端微信瀏覽器`) ![](https://img.itqaq.com/art/content/98716589d76d5f7951b341213d7113d5.png) **二、Scope 參數(shù)錯(cuò)誤或沒(méi)有 Scope 權(quán)限** 可能的原因如下: 1、 使用的是個(gè)人訂閱號(hào),訂閱號(hào)沒(méi)有權(quán)限使用網(wǎng)頁(yè)授權(quán) 2、使用的服務(wù)號(hào),沒(méi)有認(rèn)證或認(rèn)證已過(guò)期 ![](https://img.itqaq.com/art/content/5a90d92c80e0e89ace6bf79ba104d52f.png) **三、redirect_uri 參數(shù)錯(cuò)誤** 授權(quán)回調(diào)頁(yè)面域名配置錯(cuò)誤,登陸公眾號(hào)平臺(tái),在 “設(shè)置與開(kāi)發(fā)-公眾號(hào)設(shè)置-功能設(shè)置-網(wǎng)頁(yè)授權(quán)域名” 中檢查域名配置是否正確 ![](https://img.itqaq.com/art/content/0b7811eadca731848059cfa1e05c5993.png)