小程序(公眾號)授權給第三方平臺流程梳理和實現

image.png

整體流程

  • 在第三方平臺應用上點擊授權


    image.png
  • 進入授權頁面


    image.png
  • 彈出微信授權頁面,下方會顯示第三方應用的基本信息


    image.png
  • 帳號管理員掃碼,選擇要授權的賬號,進行授權(可自定義權限)
    image.png
  • 是否授權成功,回調頁面顯示

技術實現

第三方平臺方獲取預授權碼(pre_auth_code)

接入在第三方平臺應用上點擊授權的時候會獲取授權的預授權碼(pre_auth_code),有效期為10分鐘。

  • 調用接口地址

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

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

  • 參數
{
  "component_appid": "appid_value" 
}

后端返回參數,前端拼裝請求微信url

加入授權頁面的時候,前端將后端返回的數據進行組織,點擊組裝后的url調整按鈕,就可以彈出授權窗口。(微信做了限制,只能在第三方平臺在設置的回調url地址才可以訪問,其他本地地址無效

image.png

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

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

用戶授權,同意授權

用戶進入第三方平臺授權頁后,需要確認并同意將自己的公眾號或小程序授權給第三方平臺方,完成授權流程。此時在微信上,公眾號已經授權給第三方平臺了,在公眾號平臺上可以看到授權平臺。然后第三方平臺需要拿到公眾號的基本信息、授權信息和執行權限,需要回調地址進行處理、保存授權信息(access_token和refresh_token)。

回調地址處理授權信息

這個回調地址是在第三方平臺上設置的,拿到授權碼(auth_code)后,使用授權碼換取公眾號或小程序的接口調用憑據和授權信息。

  • 調用接口為:

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

  • 參數
{
  "component_appid":"appid_value" ,  //第三方平臺 appid
  "authorization_code": "auth_code_value"  //授權碼
}

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

  • 調用接口為:

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

  • 參數
{
  "component_appid":"appid_value" ,  //第三方平臺 appid
  "authorizer_appid": "auth_code_value"  //授權方 appid
}

拿到信息后你就可以保存到數據庫里了,整個微信公眾號授權的流程就結束了,后續根據各自業務對授權信息和帳號信息進行其他業務處理就ok。

代碼示例

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

PS:這里的代碼僅僅只是把整個業務流程寫在一起,方便閱讀,實際場景中代碼當然不會這樣子寫

  • 獲取預授權碼
/**
     * Created by 沙蒿.
     * @desc 獲取預授權碼pre_auth_code,有效期10分鐘
     */
    public function getWxPreAuthCode()
    {
        $authType = $this->request()->getParams('auth_type') ?? 1;
        //1公眾號授權,2小程序授權
        if (empty($authType) || !in_array($authType, [1, 2])) {
            return $this->error(100201);
        }
        //獲取第三方平臺接口調用憑證
        $comAccToken = $this->getComAccessToken();
        //請求微信服務器獲取預授權碼url地址
        $url         = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=' . $comAccToken;
        // 獲取授權請求二維碼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;
    }
  • 回調地址處理授權信息
/**
     * Created by 沙蒿.
     * @desc 微信授權
     * 授權后回調URI,得到授權碼(authorization_code)和過期時間10分鐘,使用授權碼換取公眾號或小程序的接口調用憑據和授權信息
     */
    public function wxOAuth()
    {
        //接收授權碼auth_code
        $code = $this->request()->getParams('auth_code');
        //校驗參數
        if (empty($code)) {
            return $this->error(100202);
        }
        //獲取第三方平臺的接口調用憑證
        $comAccToken = $this->getComAccessToken();
        //使用授權碼換取公眾號或小程序的接口調用憑據和授權信息
        $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
        ]);
        //獲取授權信息
        $authInfo        = $authInfo['authorization_info'];
        $authorizerAppId = $authInfo['authorizer_appid']; //授權方appid
        //獲取授權方的帳號基本信息
        $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
        ]);
        //保存授權信息和帳號信息
        $this->saveWxAuth($authInfo, $appInfo);
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。