Title_公众号授权给开放平台(Java版)

来源:互联网 发布:淘宝网一键复制宝贝 编辑:程序博客网 时间:2024/06/08 04:40

微信公众号授权给微信开放平台
微信公众号即运营商,微信开放平台即本人所在公司。
当微信公众号授权给微信开放平台时,我们就能帮助微信公众号去实现他们所想实现的内容,现在运营商想要实现微信支付,我们就替他实现微信支付。
友情链接:微信授权步骤
这次按着微信的步骤来—>

步骤一:获取第三方平台access_token
根据上一篇文章中获得的推送信息,即component_verify_ticket,以及component_appid、component_appsecret即可给微信发送POST请求,具体示例如下

/** * 1、获取第三方平台access_token * @param appid * @param appscret * @return kY9Y9rfdcr8AEtYZ9gPaRUjIAuJBvXO5ZOnbv2PYFxox__uSUQcqOnaGYN1xc4N1rI7NDCaPm_0ysFYjRVnPwCJHE7v7uF_l1hI6qi6QBsA * @throws WexinReqException*/public static String getAccessToken(ApiComponentToken apiComponentToken) throws WexinReqException{    String component_access_token = "";    String requestUrl = api_component_token_url;    JSONObject obj = JSONObject.fromObject(apiComponentToken);    JSONObject result = WxstoreUtils.httpRequest(requestUrl, "POST", obj.toString());    if (result.has("errcode")) {        logger.error("获取第三方平台access_token!errcode=" + result.getString("errcode") + ",errmsg = " + result.getString("errmsg"));        throw new WexinReqException("获取第三方平台access_token!errcode=" + result.getString("errcode") + ",errmsg = " + result.getString("errmsg"));    } else {        component_access_token = result.getString("component_access_token");    }    return component_access_token;}

具体代码以及在org.jeewx.api.JwThirdAPI详细给出,详细内容可以去看下、或者直接copy。

通过第一步我们得到component_access_token
步骤二:获取预授权码
该API用于获取预授权码。预授权码用于公众号授权时的第三方平台方安全验证。
请一定根据上面给出的微信授权步骤去实现 源码在JwThirdAPI中有,源码在第一篇文章有连接下载。

步骤三:使用授权码换取公众号的授权信息
该API用于使用授权码换取授权公众号的授权信息,并换取authorizer_access_token和authorizer_refresh_token。 授权码的获取,需要在用户在第三方平台授权页中完成授权流程后,在回调URI中通过URL参数提供给第三方平台方。
这个时候会发现有一个授权码?这个授权码的获取是从哪里来的,这里给出连接:微信公众号的授权流程
这里注意连接中的第三点即可,在当中第三点的第三点是 发布后修改和覆盖现网
第三方平台方可以在自己的网站首页中放置“微信公众号登录授权”的入口,引导公众号运营者进入第三方平台授权页。网址为https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx,该网址中第三方平台方需要提供第三方平台方appid、预授权码和回调URI
主要需要这个网址 我们会发现其中的前两个参数都已经在前面的步骤1 、2中得到,至于redirect_uri结合步骤三中的回调URI即可,下面附上部分代码

mui只是一个框架 用JQ也是可以的wx-authorize.htmlmui.init();        mui.ajax(myDomain + 'api/WxThird_getInfoForAuthorization.jsp',{            data:{},            dataType:'json',            type:'post',            timeout:10000,            success:function(data) {                if(data.error == 0) {                    console.log("info="+JSON.stringify(data));                    var url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage'                    url += '?component_appid=' + data.component_appid;                    url += '&pre_auth_code=' + data.pre_auth_code;                    url += '&redirect_uri=' + data.redirect_uri;                    var obj = document.getElementById('authorize');                    obj.setAttribute('href', url);                }            },            error:function(xhr, type, errorThrown) {                alert(xhr + ',' + type + ',' + errorThrown);            }        })

关于上面这个页面 是我们自己添加的 可以不进行此页面 直接把参数添加到微信提供的网址即可

    Map<String,Object> map = new HashMap<String,Object>();    WxPoint point = new WxPoint();    String preAuthCode = point.getPreAuthCode();    //返回页面    String redirectUrl = "http://www.iskyct.cn/api/WxThird_getInfoUrl.jsp";    String componentAppid = "wxxxxxxxxxxxxxxx";    map.put("component_appid",componentAppid);    map.put("pre_auth_code",preAuthCode);    map.put("redirect_uri",redirectUrl);    out.print(map);

接下来将要跳转到步骤三

String authCode = request.getParameter("auth_code").toString();    String expiresIn = request.getParameter("expires_in").toString();       String componentAppid = "wxxxxxxxxxxxxxx";    JSONObject result = JwThirdAPI.getApiQueryAuthInfo(componentAppid,authCode,WxPoint.getAccessToken());    WxPoint.PostCustomerInfo(result);

这样子 就完全得到了 第三方授权信息

步骤四:获取(刷新)授权公众号的令牌(这个步骤非常的重要)
该API用于在授权方令牌(authorizer_access_token)失效时,可用刷新令牌(authorizer_refresh_token)获取新的令牌。请注意,此处token是2小时刷新一次,开发者需要自行进行token的缓存,避免token的获取次数达到每日的限定额度。缓存方法可以参考:http://mp.weixin.qq.com/wiki/2/88b2bf1265a707c031e51f26ca5e6512.html
我们的缓存方法是存在json文件中,因为还并没有很多的运营商,如果运营商多了的话,存在数据库中看起来更加靠谱。

除了以上内容 接下来即将是微信JDK的接入 以及实现其功能 都很简单

1 0
原创粉丝点击