小程序(公眾號(hào))授權(quán)給第三方平臺(tái)流程梳理和實(shí)現(xiàn)

image.png

整體流程

  • 在第三方平臺(tái)應(yīng)用上點(diǎn)擊授權(quán)


    image.png
  • 進(jìn)入授權(quán)頁(yè)面


    image.png
  • 彈出微信授權(quán)頁(yè)面,下方會(huì)顯示第三方應(yīng)用的基本信息


    image.png
  • 帳號(hào)管理員掃碼,選擇要授權(quán)的賬號(hào),進(jìn)行授權(quán)(可自定義權(quán)限)
    image.png
  • 是否授權(quán)成功,回調(diào)頁(yè)面顯示

技術(shù)實(shí)現(xiàn)

第三方平臺(tái)方獲取預(yù)授權(quán)碼(pre_auth_code)

接入在第三方平臺(tái)應(yīng)用上點(diǎn)擊授權(quán)的時(shí)候會(huì)獲取授權(quán)的預(yù)授權(quán)碼(pre_auth_code),有效期為10分鐘。

  • 調(diào)用接口地址

POST https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=COMPONENT_ACCESS_TOKEN

在調(diào)用此接口前,需要先獲取第三方平臺(tái)的令牌(也叫接口調(diào)用憑證component_access_token)

  • 參數(shù)
{
  "component_appid": "appid_value" 
}

后端返回參數(shù),前端拼裝請(qǐng)求微信url

加入授權(quán)頁(yè)面的時(shí)候,前端將后端返回的數(shù)據(jù)進(jìn)行組織,點(diǎn)擊組裝后的url調(diào)整按鈕,就可以彈出授權(quán)窗口。(微信做了限制,只能在第三方平臺(tái)在設(shè)置的回調(diào)url地址才可以訪問(wèn),其他本地地址無(wú)效

image.png

  • 后端返回的參數(shù)
  'component_appid' => 'XXX',  //第三方平臺(tái)app_id
  'pre_auth_code'   => 'pre_auth_code'  //  預(yù)授權(quán)碼
  'redirect_uri'    => 'https://mp.weixin.qq.com/cgi-bin/componentloginpage',  //拼裝的URL地址
  'auth_type'       =>  1,  //1 2 3  要授權(quán)的帳號(hào)類型
  'biz_appid'       =>  'xxx'  //指定授權(quán)唯一的小程序或公眾號(hào)
  • 拼裝示例

https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx&auth_type=xxx

用戶授權(quán),同意授權(quán)

用戶進(jìn)入第三方平臺(tái)授權(quán)頁(yè)后,需要確認(rèn)并同意將自己的公眾號(hào)或小程序授權(quán)給第三方平臺(tái)方,完成授權(quán)流程。此時(shí)在微信上,公眾號(hào)已經(jīng)授權(quán)給第三方平臺(tái)了,在公眾號(hào)平臺(tái)上可以看到授權(quán)平臺(tái)。然后第三方平臺(tái)需要拿到公眾號(hào)的基本信息、授權(quán)信息和執(zhí)行權(quán)限,需要回調(diào)地址進(jìn)行處理、保存授權(quán)信息(access_token和refresh_token)。

回調(diào)地址處理授權(quán)信息

這個(gè)回調(diào)地址是在第三方平臺(tái)上設(shè)置的,拿到授權(quán)碼(auth_code)后,使用授權(quán)碼換取公眾號(hào)或小程序的接口調(diào)用憑據(jù)和授權(quán)信息。

  • 調(diào)用接口為:

POST https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=COMPONENT_ACCESS_TOKEN

  • 參數(shù)
{
  "component_appid":"appid_value" ,  //第三方平臺(tái) appid
  "authorization_code": "auth_code_value"  //授權(quán)碼
}

返回是僅僅是授權(quán)信息(authorization_info)。authorizer_appid,authorizer_access_token,expires_in,authorizer_refresh_token以及權(quán)限id集這些數(shù)據(jù)。尚未獲得公眾號(hào)一些基本帳號(hào)信息(公眾號(hào)名稱、頭像等等),這時(shí)候需要去獲取授權(quán)方的帳號(hào)基本信息。

  • 調(diào)用接口為:

POST https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token==COMPONENT_ACCESS_TOKEN

  • 參數(shù)
{
  "component_appid":"appid_value" ,  //第三方平臺(tái) appid
  "authorizer_appid": "auth_code_value"  //授權(quán)方 appid
}

拿到信息后你就可以保存到數(shù)據(jù)庫(kù)里了,整個(gè)微信公眾號(hào)授權(quán)的流程就結(jié)束了,后續(xù)根據(jù)各自業(yè)務(wù)對(duì)授權(quán)信息和帳號(hào)信息進(jìn)行其他業(yè)務(wù)處理就ok。

代碼示例

$this->request()->getParams() 是封裝好的獲取參數(shù)的方法,可自行替代
getComAccessToken() 是封裝好的獲取第三方接口調(diào)用憑證的方法,可自行替代
httpsCurl() 是封裝好的請(qǐng)求微信的方法,可自行替代
WX_APP_ID 是全局參數(shù)第三方平臺(tái)的app_id

PS:這里的代碼僅僅只是把整個(gè)業(yè)務(wù)流程寫在一起,方便閱讀,實(shí)際場(chǎng)景中代碼當(dāng)然不會(huì)這樣子寫

  • 獲取預(yù)授權(quán)碼
/**
     * Created by 沙蒿.
     * @desc 獲取預(yù)授權(quán)碼pre_auth_code,有效期10分鐘
     */
    public function getWxPreAuthCode()
    {
        $authType = $this->request()->getParams('auth_type') ?? 1;
        //1公眾號(hào)授權(quán),2小程序授權(quán)
        if (empty($authType) || !in_array($authType, [1, 2])) {
            return $this->error(100201);
        }
        //獲取第三方平臺(tái)接口調(diào)用憑證
        $comAccToken = $this->getComAccessToken();
        //請(qǐng)求微信服務(wù)器獲取預(yù)授權(quán)碼url地址
        $url         = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=' . $comAccToken;
        // 獲取授權(quán)請(qǐng)求二維碼url地址
        $reqUrl      = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage';
        $preAuthCode = CommonService::getInstance()->httpsCurl($url, 'post', 'json', [
            'component_appid' => WX_APP_ID
        ]);
        //組裝格式,返回
        $result = [
            'component_appid' => WX_APP_ID,
            'pre_auth_code'   => $preAuthCode['pre_auth_code'],
            'redirect_uri'    => $reqUrl,
            'auth_type'       => $authType,
        ];
        if (!empty($bizAppId)) {
            $result['biz_appid'] = $bizAppId;
        }
        return $result;
    }
  • 回調(diào)地址處理授權(quán)信息
/**
     * Created by 沙蒿.
     * @desc 微信授權(quán)
     * 授權(quán)后回調(diào)URI,得到授權(quán)碼(authorization_code)和過(guò)期時(shí)間10分鐘,使用授權(quán)碼換取公眾號(hào)或小程序的接口調(diào)用憑據(jù)和授權(quán)信息
     */
    public function wxOAuth()
    {
        //接收授權(quán)碼auth_code
        $code = $this->request()->getParams('auth_code');
        //校驗(yàn)參數(shù)
        if (empty($code)) {
            return $this->error(100202);
        }
        //獲取第三方平臺(tái)的接口調(diào)用憑證
        $comAccToken = $this->getComAccessToken();
        //使用授權(quán)碼換取公眾號(hào)或小程序的接口調(diào)用憑據(jù)和授權(quán)信息
        $queryAuthUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=' . $comAccToken;
        $authInfo     = CommonService::getInstance()->httpsCurl($queryAuthUrl, 'post', 'json', [
            'component_appid'    => WX_APP_ID,
            'authorization_code' => $code
        ]);
        //獲取授權(quán)信息
        $authInfo        = $authInfo['authorization_info'];
        $authorizerAppId = $authInfo['authorizer_appid']; //授權(quán)方appid
        //獲取授權(quán)方的帳號(hào)基本信息
        $authorizerInfoUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=' . $comAccToken;
        $appInfo           = CommonService::getInstance()->httpsCurl($authorizerInfoUrl, 'post', 'json', [
            'component_appid'  => WX_APP_ID,
            'authorizer_appid' => $authorizerAppId
        ]);
        //保存授權(quán)信息和帳號(hào)信息
        $this->saveWxAuth($authInfo, $appInfo);
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容