通过authorizer_refresh_token更新authorizer_access_token(基于Authorizer_appid)

获取(刷新)授权公众号或小程序的接口调用凭据(令牌)

该API用于在授权方令牌(authorizer_access_token)失效时,可用刷新令牌(authorizer_refresh_token)获取新的令牌。请注意,此处token是2小时刷新一次,开发者需要自行进行token的缓存,避免token的获取次数达到每日的限定额度。缓存方法可以参考:http://mp.weixin.qq.com/wiki/2/88b2bf1265a707c031e51f26ca5e6512.html

当换取authorizer_refresh_token后建议保存。

public void updateToken() {
    List<AuthorizerInfo> list = authorizerDao.findAllAuthorizer();
    for (AuthorizerInfo authorizerInfo :list){
        AuthorizerInfo authorizer = authorizerDao.findAuthorizerByAppId(authorizerInfo.getAuthorizer_appid());
        ConfigInfo config = configDao.findConfigById();
        String token = config.getComponent_access_token();
        String data;
        try {
            Map<String, Object> map = new HashMap<>();
            if (authorizer!=null) {
                map.put("component_appid", COMPONENT_APPID);
                map.put("authorizer_appid", authorizer.getAuthorizer_appid());
                map.put("authorizer_refresh_token", authorizer.getAuthorizer_refresh_token());
                data = HttpRequestor.doPost(
                        "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token="
                                + token,
                        GsonUtil.objectToJson(map));
                Map<String, Object> resultData = GsonUtil.parseJsonWithGson(data, Map.class);
                String str = "\"";
                String authorizer_access_token = GsonUtil.objectToJson(resultData.get("authorizer_access_token"))
                        .replace(str, "");
                String authorizer_refresh_token = GsonUtil.objectToJson(resultData.get("authorizer_refresh_token"))
                        .replace(str, "");
                if(authorizer_access_token!=null&&authorizer_refresh_token!=null){
                    authorizer.setAuthorizer_access_token(authorizer_access_token);
                    authorizer.setAuthorizer_refresh_token(authorizer_refresh_token);
                    authorizerDao.updateAuthorizerByAppId(authorizer);
                }else {
                    logger.error(authorizer.getAuthorizer_appid()+"为null.....");
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用PHP完成微信第三方平台授权,你可以按照以下步骤进行操作: 1. 首先,你需要注册成为微信开放平台的开发者,并创建一个第三方平台应用。 2. 在创建应用后,你会得到一个AppIDAppSecret,这些是与微信平台进行通信的凭证。 3. 在你的PHP项目中,你需要使用curl库或其他HTTP请求库来发送HTTP请求到微信开放平台的API接口。 4. 首先,你需要获取预授权码(pre_auth_code)。发送GET请求到以下接口: ``` https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=xxx ``` 其中,component_access_token是通过调用获取第三方平台component_access_token接口获取的。 5. 获取到预授权码后,你可以使用预授权码和你的AppID生成授权链接,引导用户进入授权页面: ``` https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxx&pre_auth_code=xxx&redirect_uri=xxx ``` 其中,component_appid是你的AppID,pre_auth_code是上一步获取的预授权码,redirect_uri是用户授权后的回调URL。 6. 用户在授权页面确认授权后,会跳转到你指定的回调URL,并携带授权码(authorization_code)参数。 7. 在回调URL对应的PHP页面中,你需要解析URL中的授权码参数,并使用授权码发送POST请求到以下接口,获取授权令牌(authorizer_access_token)和刷新令牌(authorizer_refresh_token): ``` https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=xxx ``` 其中,component_access_token是通过调用获取第三方平台component_access_token接口获取的。 8. 获取到授权令牌和刷新令牌后,你可以使用它们来调用微信开放平台的其他API接口,完成后续的操作。 以上是使用PHP完成微信第三方平台授权的基本流程,具体的实现细节可能会因具体需求而有所不同。在实际开发中,你还需要处理授权过期、刷新令牌等情况,以保证授权的有效性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值