失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 微信公众号授权第三方平台

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

时间:2024-06-02 18:07:39

相关推荐

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

本文是自己做授权时的整理,因为微信的官方文档有点乱,不仔仔细细的话,很容易出错。如果文中有写的不对的,请大家指出来,方便你我他。另:项目使用的是jfinal。以下是步骤:

一、创建第三方平台:

二、第三方平台创建审核后,微信会推送消息到授权实践URL上 。消息是xml类型,且是加密的。也会将解密需要的参数都传过来。

加密解密参数

signature=0c81c9d03501efce8c275f04361c704904c0a4af timestamp=1492740992 nonce=1801968109 encrypt_type=aes msg_signature=8aa45e464cddf2d37d9dba9bd12787171a9dd629

收到的消息体

<xml><AppId><![CDATA[wx65ad4bd28dd98d69]]></AppId><Encrypt><![CDATA[PKhoW+WCpncp1U5fx3tzeR7ymD9VcNGiVza1QpMXjAC4fSR3tclvdTyEVHn1/0+Zl0DzZH7VwwIylSftpnAizofxgdC7BDYmBdP0WTyh1MdTK7E/HNbSgoC6a0RWCc/RotmKRhznvtFOEoPjhfIB79yePPe6oXT4/iHqcCIFgipFbCRfr3vxIyvyJJ1YKurhQCSjne5eGsxr6boTUb9bu3GulQcdNwKrXlItcE/EpOB1aBsuE/FTnLop0SA==]]></Encrypt></xml>

解密结果

{AppId=wx65ad4bd8888888, CreateTime=1492751128, 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./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./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./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./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。在快到两小时前几分钟,用它重新获取上边的tokenString authorizer_refresh_token = authoInfo.getString("authorizer_refresh_token");//授权公众号的AppIdString authorizer_appid = authoInfo.getString("authorizer_appid");redirect("你的业务页面);}}

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

如果觉得《微信公众号授权第三方平台》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。