官方流程如下
實際上這個流程圖並不完整
實際流程
- 獲取推送的component_verify_ticket,開放平台會每10分鍾推送到授權事件接收URL里
- 獲取component_access_token,使用上一步獲取到的ticket,向開放平台請求https://api.weixin.qq.com/cgi-bin/component/api_component_token,這個token有效期兩小時
- 獲取預授權碼pre_auth_code,使用component_access_token,向開放平台請求https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=%s
- 拼接授權回調url https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx&auth_type=xxx
- 在回調里向開放平台請求https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=%s,獲取公眾號授權,取得公眾號id,這里還會返回公眾的access_token,和refresh_token
- 在回調里向開放平台請求https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=%s,使用上一步的公眾號id,獲取公眾號信息
常見錯誤
- 請確認授權入口頁所在域名,與授權后回調頁面所在域名相同
授權回調頁要在服務器所在的那台機器上,可以簡單的做個網頁,在網頁里跳轉到授權頁