天天看点

微信公众号授权第三方平台

本文是自己做授权时的整理,因为微信的官方文档有点乱,不仔仔细细的话,很容易出错。如果文中有写的不对的,请大家指出来,方便你我他。另:项目使用的是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("你的业务页面);}
    }
           

剩余的事情就很简单了,按需求自己处理吧!!!!!!!!!!!!!!