天天看點

微信公衆号授權第三方平台

本文是自己做授權時的整理,因為微信的官方文檔有點亂,不仔仔細細的話,很容易出錯。如果文中有寫的不對的,請大家指出來,友善你我他。另:項目使用的是jfinal。以下是步驟:

一、建立第三方平台:

微信公衆号授權第三方平台

二、第三方平台建立稽核後,微信會推送消息到授權實踐URL上 。消息是xml類型,且是加密的。也會将解密需要的參數都傳過來。

加密解密參數

signature=c81c9d03501efce8c275f04361c704904c0a4af  timestamp=  nonce=  encrypt_type=aes  msg_signature=aa45e464cddf2d37d9dba9bd12787171a9dd629  
           
收到的消息體
           
解密結果
           
{AppId=wx65ad4bd8888888, CreateTime=, ComponentVerifyTicket=ticket@@@hE_Vz8PYsv01BDPa_nMA6OqtUwmT-dEfiwlelHOx_32542466gpdp1_AyEHJl4WfnjGJb42-gA, InfoType=component_verify_ticket}
           

需要的資料是ComponentVerifyTicket (擷取component_access_token 需要),InfoType (是否授權及授權更新)的值。

微信公衆号授權第三方平台

三、根據ComponentVerifyTicket 擷取component_access_token

Map<String, String> map = new HashMap<String, String>();
        map.put("component_appid", thirdAppid);
        map.put("component_appsecret", secret);
        map.put("component_verify_ticket", component_verify_ticket);
        String parma = JsonKit.toJson(map);
        JSONObject jsonObject = new JSONObject(HttpKit.post("https://api.weixin.qq.com/cgi-bin/component/api_component_token", parma));
        String component_access_token = "";
        try {
            component_access_token = jsonObject.getString("component_access_token");

        } catch (JSONException e) {
            System.out.println("Error:" + e.getMessage());
        }
           

四、根據component_access_token擷取預授權碼,并進入授權頁

Map<String, String> map = new HashMap<String, String>();
        map.put("component_appid", "第三方平台AppId");
        String parma = JsonKit.toJson(map);
        JSONObject jsonObject = new JSONObject(HttpKit.post("https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token="+ component_access_token, parma));
       //擷取預授權碼
        String pre_auth_code = "";
        try {
            pre_auth_code = jsonObject.getString("pre_auth_code");
        } catch (JSONException e) {
        }
        String openAuthUrl = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid="+ thirdAppId + "&pre_auth_code=" + pre_auth_code + "&redirect_uri=" + "回調url"+ "第三方平台AppId";
//跳轉到掃碼授權頁
        redirect(openAuthUrl);
           

五、授權成功,拿到授權碼auth_code,并用授權碼和第三方AppId擷取authorizer_access_token和authorizer_refresh_token。

//拿到auth_code,說明授權成功。(此處拿參數的字段是"**auth_code**",)
      String authorization_code = getPara("auth_code");
            Map<String, String> map = new HashMap<String, String>();
        map.put("component_appid", thirdAppId);
        map.put("authorization_code", authorization_code);
        String parma = JsonKit.toJson(map);
        JSONObject jsonObject = new JSONObject(HttpKit.post("https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token="+ component_access_token, parma));
        System.out.println("JSON 這是資料這是資料這是資料這是資料" + jsonObject);
        JSONObject authoInfo = jsonObject.getJSONObject("authorization_info");
        //操作公衆号的token,有效時間2小時。
        String authorizer_access_token = authoInfo.getString("authorizer_access_token");
        //重新整理token。在快到兩小時前幾分鐘,用它重新擷取上邊的token
        String authorizer_refresh_token = authoInfo.getString("authorizer_refresh_token");
//授權公衆号的AppId
        String authorizer_appid = authoInfo.getString("authorizer_appid");
        redirect("你的業務頁面);}
    }
           

剩餘的事情就很簡單了,按需求自己處理吧!!!!!!!!!!!!!!