微信代公众号授权登录踩过的坑

代公众号发起网页授权

在公众号授权托管给第三方平台后,第三方平台可以根据官方文档相关说明,代替授权公众号发起网页授权。

官方文档

GitHub

下图是我开发测试截图,比邻用户中心是第三方公众号,此时已授权托管给我们自己的微信公众平台,此时发起网页授权,用户同意后即可获取用户相关信息。

微信代公众号授权登录踩过的坑_第1张图片

授权流程:

1、根据appid获取预授权码code

2、根据code获取accessToken

3、根据accessToken获取userInfo

可直接去GitHub上下载WxJava-master,查看weixin-java-open中的代码

比邻用户中心需要在我司的微信公众平台完成第三方平台授权后,才能获取预授权码code,否则会scope参数错误或没有scope权限

检查授权后的权限,网页账号,网页授权,回调页域名,否则也会报scope参数错误或没有scope权限

用户在微信客户端打开以下链接

https://open.weixin.qq.com/connect/oauth2/authorize?appid=XXXXX&redirect_uri=XXXXX&response_type=code&scope=snsapi_userinfo&state=wechat_component_login&component_appid=XXXXX#wechat_redirect

微信代公众号授权登录踩过的坑_第2张图片

用户允许授权后,将会重定向到redirect_uri的网址上,并且带上code, state以及appid

注意,redirect_uri需要encode编码,同时需要在微信公众平台上配置回调函数,否则会报redirect_uri 参数错误

然后通过code换取accessToken

https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=APPID&code=CODE&grant_type=authorization_code&component_appid=COMPONENT_APPID&component_access_token=COMPONENT_ACCESS_TOKEN

微信代公众号授权登录踩过的坑_第3张图片

component_access_token为我司的微信公众平台access_token,代公众号第三方授权完成后存储在redis中,

WxMpOAuth2AccessToken accessToken = wxOpenComponentService.oauth2getAccessToken(appId, authCode);

这里wxOpenComponentService是通过@Bean注解注入

    @Bean
    @ConditionalOnMissingBean
    public WxOpenComponentService wxOpenComponentService() {
        WxOpenConfigStorage wxOpenConfigStorage = new WxOpenInRedisConfigStorageImpl(stringRedisTemplate);
        wxOpenConfigStorage.setComponentAppId(wechatProperties.getMpAppId());
        wxOpenConfigStorage.setComponentAppSecret(wechatProperties.getMpAppSecret());

        WxOpenService wxOpenService = new WxOpenServiceImpl();
        wxOpenService.setWxOpenConfigStorage(wxOpenConfigStorage);

        WxOpenComponentService wxOpenComponentService = new WxOpenComponentServiceImpl(wxOpenService);

        return wxOpenComponentService;
    }

 

跟进去可看到oauth2getAccessToken方法通过

this.getWxOpenConfigStorage().getComponentAppId()获取componentAppId

和this.getComponentAccessToken(false);方法获取componentAccessToken

跟进去

public String getComponentAccessToken(boolean forceRefresh) throws WxErrorException {
        if(this.getWxOpenConfigStorage().isComponentAccessTokenExpired() || forceRefresh) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("component_appid", this.getWxOpenConfigStorage().getComponentAppId());
            jsonObject.addProperty("component_appsecret", this.getWxOpenConfigStorage().getComponentAppSecret());
            jsonObject.addProperty("component_verify_ticket", this.getWxOpenConfigStorage().getComponentVerifyTicket());
            String responseContent = this.getWxOpenService().post("https://api.weixin.qq.com/cgi-bin/component/api_component_token", jsonObject.toString());
            WxOpenComponentAccessToken componentAccessToken = WxOpenComponentAccessToken.fromJson(responseContent);
            this.getWxOpenConfigStorage().updateComponentAccessTokent(componentAccessToken);
        }

        return this.getWxOpenConfigStorage().getComponentAccessToken();
    }

这里getComponentAccessToken()就是基于redis的微信配置,存储获取componentAccessToken等

微信代公众号授权登录踩过的坑_第4张图片

最后通过

wxMpService.oauth2getUserInfo(accessToken, "zh_CN");

获取WxMpUser用户信息

参考博客

你可能感兴趣的:(日常)