微信网页认证,code,access_token

微信网页认证

业务场景:获取微信用户的信息,如:微信名、微信头像、地址、是否关注公众号。等等

准备条件

1.内网穿透(我使用的是natapp也可以使用花生壳之类的)
https://natapp.cn/article/natapp_newbie
2.注册微信开发测试号:
https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
注意:这里的url和Token填写公众号的信息,下图
注意:这里的url和Token填写公众号的信息,如果还没有公众号信息,url可以填写内网穿透得到的域名,Token可以随便填写
在这里插入图片描述

在这里插入图片描述
找到微信测试号下方的 网页帐号–》 网页授权获取用户基本信息,点击修改
在这里插入图片描述
上方图片的(JS接口安全域名修改)和 ( 网页授权获取用户基本信息) 分别填入内网穿透的地址

1 第一步:用户同意授权,获取code

获取code链接:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_re

在这里插入图片描述

查看官方文档


//填写微信测试号的信息,上线改为公众号的信息
    private static final String APPID="";
    private static final String APPSECRET ="";

//得到code
    @RequestMapping(value = "/wxLogin", method = RequestMethod.GET)
    public String wxLogin( HttpServletRequest request, HttpServletResponse response)throws ParseException, UnsupportedEncodingException {
        //System.out.println("第一步:获取code方法执行");
        //这个地址是成功后的回调地址,域名必须和公众号中配置的域名一致
        String backUrl="填写内网穿透得到的域名/access_token";

        // 第一步:用户同意授权,获取code 这里采用的默认方式
        String url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ APPID
                + "&redirect_uri="
                + URLEncoder.encode(backUrl, "utf-8")
                + "&response_type=code"
                + "&scope=snsapi_base"
                + "&state=STATE#wechat_redirect";
//        logger.info("forward重定向地址{" + url + "}");
        return "redirect:" + url ;
    }

    //根据得到的code过去access_token
    @RequestMapping(value="/access_token" , method = RequestMethod.GET)
    public void WxIndex(Model model, HttpServletRequest req, HttpServletResponse resp ,String code) throws ServletException, IOException {

        System.out.println("得到的code为:  "+code);
		
    }

2 第二步:通过code换取网页授权access_token

获取路径:https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

在这里插入图片描述

在这里插入图片描述

 //根据得到的code过去access_token
    @RequestMapping(value="/access_token" , method = RequestMethod.GET)
    public String WxIndex(Model model, HttpServletRequest req, HttpServletResponse resp ,String code) throws ServletException, IOException {

        //System.out.println("code:  "+code);

        //里面包含openid 和 accrss_token
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APPID
                + "&secret="+APPSECRET
                + "&code="+code
                + "&grant_type=authorization_code";
      
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);

        JSONObject fromObject = JSONObject.fromObject(response);
     String  openid = fromObject.getString("openid");

     String access_token = fromObject.getString("access_token");
return "你的业务页面";
    }

3 第三步:刷新access_token(如果需要)

我这里没写

4 第四步:拉取用户信息(需scope为 snsapi_userinfo)

获取链接: https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

在这里插入图片描述

正确返回如下:
在这里插入图片描述
参数说明:
在这里插入图片描述

 @RequestMapping(value="/subscribe" , method = RequestMethod.GET)
    public String getSubscribe() throws UnsupportedEncodingException {
	//获取用户信息
        String url3="https://api.weixin.qq.com/cgi-bin/user/info?access_token="
                + access_token
                + "&openid="
                + openid
                + "&lang=zh_CN";
        RestTemplate restTemplate = new RestTemplate();
        String response3 = restTemplate.getForObject(url3, String.class);

        //处理得到json里面的乱码
        String json = new String(response3.getBytes("ISO-8859-1"), "UTF-8");
        System.out.println(json);


        JSONObject fromObject = JSONObject.fromObject(json);
        String subsecride = fromObject.getString("subscribe");	//是否关注公众号
        
        String headimgurl = fromObject.getString("headimgurl");	//用户头像地址
        return "业务页面";
    }

以上也有写得不好的地方,多多指教
qq:2386271849 相互学习

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值