NutzCN Logo
问答 微信用户鉴权获取openid,code可以得到Openid为空
发布于 1797天前 作者 wx_2jk6i7mpius707s625cs 4384 次浏览 复制 上一个帖子 下一个帖子
标签:
@At
    public String  getCode(@Param("code")String code) {
    	PropertiesProxy conf =new PropertiesProxy();
        logger.info(code);
        conf.setPaths("/custom");
        String appid = conf.get("weixin.appid");
        String appsecret = conf.get("weixin.appsecret");
        String fmt = "https://api.weixin.qq.com/sns/oauth2/access_token"
                + "?appid=%s"
                + "&secret=%s"
                + "&code=%s"
                + "&grant_type=authorization_code";
        String url = String.format(fmt,appid,appsecret,code);
        logger.info(url);
        String json = Http.get(url).getContent();
        logger.info("resp"+json);
        NutMap map = Json.fromJson(NutMap.class, json);
        logger.info("map"+map);
        String openid = map.getString("openid");
        logger.info("用户的标识"+openid);

        return openid;
    }
9 回复
 @At("/?/back")
    @Ok("re")
    @Filters
    public String back(String wxid, @Param("code") String code, HttpServletRequest req, HttpSession session) throws Exception {
        String wechat_goto_url = Strings.sNull(session.getAttribute("wechat_goto_url"));
        Wx_config config = wxConfigService.fetch(wxid);
        String fmt = "https://api.weixin.qq.com/sns/oauth2/access_token"
                + "?appid=%s"
                + "&secret=%s"
                + "&code=%s"
                + "&grant_type=authorization_code";

        String url = String.format(fmt, config.getAppid(), config.getAppsecret(), code);
        String json = Http.get(url).getContent();
        NutMap map = Json.fromJson(NutMap.class, json);
        String openid = map.getString("openid");
        WxApi2 wxApi2 = wxService.getWxApi2(wxid);
        Wx_user usr = wxUserService.fetch(Cnd.where("openid", "=", openid).and("wxid", "=", wxid));
        WxResp resp = wxApi2.user_info(openid, "zh_CN");
        if (usr == null) {
            usr = Json.fromJson(Wx_user.class, Json.toJson(resp.user()));
            if (usr != null && usr.getNickname() != null) {
                usr.setNickname(Strings.sNull(EmojiParser.parseToAliases(usr.getNickname(), EmojiParser.FitzpatrickAction.REMOVE)));
                usr.setSubscribeAt(resp.user().getSubscribe_time());
                usr.setWxid(wxid);
                wxUserService.insert(usr);
            }
        } else {
            String id = usr.getId();
            usr = Json.fromJson(Wx_user.class, Json.toJson(resp.user()));
            usr.setNickname(EmojiParser.parseToAliases(usr.getNickname(), EmojiParser.FitzpatrickAction.REMOVE));
            usr.setOpAt(Times.getTS());
            usr.setWxid(wxid);
            usr.setId(id);
            wxUserService.updateIgnoreNull(usr);
        }
        session.setAttribute("wxid", wxid);
        session.setAttribute("openid", usr == null ? "" : usr.getOpenid());
        return "redirect:" + wechat_goto_url;
    }

我是按照这个代码走的。小纠结不知道什么情况

我的wk设置了回调url这里只需要获得code然后掉接口获取openid就可以了但是获得的openid为空

变量json和map分别输出了什么

没输出出来是什么意思群里有截图

没有输出是啥意思,json和map都是空?

控制台没有打印出来。。。

我看了一下是我的配置文件配置错了,谢谢

添加回复
请先登陆
回到顶部