微信公众号授权第三方平台
2017-04-21 19:34
190 查看
本文是自己做授权时的整理,因为微信的官方文档有点乱,不仔仔细细的话,很容易出错。如果文中有写的不对的,请大家指出来,方便你我他。另:项目使用的是jfinal。以下是步骤:
一、创建第三方平台:
二、第三方平台创建审核后,微信会推送消息到授权实践URL上 。消息是xml类型,且是加密的。也会将解密需要的参数都传过来。
加密解密参数
需要的数据是ComponentVerifyTicket (获取component_access_token 需要),InfoType (是否授权及授权更新)的值。
三、根据ComponentVerifyTicket 获取component_access_token
四、根据component_access_token获取预授权码,并进入授权页
五、授权成功,拿到授权码auth_code,并用授权码和第三方AppId获取authorizer_access_token和authorizer_refresh_token。
剩余的事情就很简单了,按需求自己处理吧!!!!!!!!!!!!!!
一、创建第三方平台:
二、第三方平台创建审核后,微信会推送消息到授权实践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.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("你的业务页面);} }
剩余的事情就很简单了,按需求自己处理吧!!!!!!!!!!!!!!
相关文章推荐
- 微信公众号第三方平台开发 授权流程
- 微信公众号授权给第三方平台时报“没有绑定公众号”
- 微信公众号第三方平台开发 授权流程
- ASP.NET之MVC 微信公众号授权给第三方平台的技术实现流程一(获取第三方平台access_token)
- php开发微信公众号 第三方平台一键绑定(授权)感言
- 微信公众号第三方开发之三创建一键登录页授权给第三方平台
- 微信公众号实现智能聊天-接入第三方智能机器人平台
- 支付宝开发平台之第三方授权登录与获取用户信息
- 使用友盟进行第三方平台的授权分享
- php微信第三方平台授权
- 微信第三方平台授权流程- java
- 微信开放平台 公众号第三方平台开发 教程三 一键登录授权给第三方平台
- 微信公众号和第三方的平台的对接方法
- 微信公众号第三方平台开发的一些教训总结
- 支付宝开发平台之第三方授权登录与获取用户信息
- 支付宝开发平台之第三方授权登录与获取用户信息
- Android平台下Facebook,Google service, 新浪微博接入第三方应用SSO授权教程
- 微信公众号预约第三方平台
- 微信第三方平台开发授权1:获取component_verify_ticket
- 微信公众号客服接口升级 支持第三方平台调用