微信登录--网页授权

微信登录

我们在使用微信公众号的时候经常会看到以下应用场景,点击公众号菜单打开一个新的网页,然后弹出一个弹框问你是否授权,通过后就能直接用微信号登录这个应用了。

这个功能就是微信的网页授权: 网页授权 | 微信开放文档 

 第一步,组装回调url

 我们要组装以下url,前面的域名都是固定的,主要修改参数值,如appid改成你应用的appid,response_type就是code,不用修改,scope常用的是snsapi_userinfo,redirect_uri就是接下去要跳转的网页地址,也就是你网站的地址

需要注意的是这个网址里面的域名需要先配置,到公众平台官网中的「设置与开发」-「功能设置」-「网页授权域名」的配置选项中,修改授权回调域名

请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加 http:// 等协议头。

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx807d86fb6b3d4fd2&redirect_uri=http%3A%2F%2Fdevelopers.weixin.qq.com&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
参数是否必须说明
appid公众号的唯一标识
redirect_uri授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理
response_type返回类型,请填写code
scope应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
state重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
#wechat_redirect无论直接打开还是做页面302重定向时候,必须带此参数
forcePopup强制此次授权需要用户弹窗确认;默认为false;需要注意的是,若用户命中了特殊场景下的静默授权逻辑,则此参数不生效

 如果你用的是测试公众号,则回调域名在这里修改,支持Ip,如果不是默认端口号需要填上端口号

最后提醒一下,只有服务号支持网页授权 

第二步,获取code

 把你组装好的url发到微信里面,然后在微信里面点击,就能打开该网页,然后点使用默认浏览器打开,复制浏览器的url,就能看到code了

第三步,后台通过code拿到openid,用户信息 

 有了code后,前端把code发往后台,后台能通过code拿到openid

获取code后,请求以下链接获取access_token:

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

参数是否必须说明
appid公众号的唯一标识
secret公众号的appsecret
code填写第一步获取的code参数
grant_type填写为authorization_code

 这个接口返回openid和access_token,有了这两个数据就能请求接口获取用户的昵称头像等数据。

 需要注意的是这个access_token是网页授权的token,只是用来获取用户信息的,跟普通token也就是用来创建公众号菜单,回复消息的token不同。

代码实现如下:

@RestController
@RequestMapping("/wx")
@Api(tags = "微信管理")
public class WxController {
    @Autowired
    private WeixinService weixinService;

    @ApiOperation(value = "根据code获取用户信息")
    @GetMapping("getUserInfoByCode")
    public WxUser getUserInfoByCode(String code) {
        return weixinService.getUserInfoByCode(code);
    }
}
@Service
public class WeixinService {
    private final static String appid = "自己的appid";
    private final static String appsecret = "自己的appsecret";

    public WxUser getUserInfoByCode(String code) {
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={}&secret={}&code={}&grant_type=authorization_code";
        url = StrUtil.format(url, appid, appsecret, code);
        String res = HttpUtil.get(url);
        JSONObject tokenInfo = handleResult(res);
        String token = tokenInfo.getString("access_token");
        String openid = tokenInfo.getString("openid");
        String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token={}&openid={}&lang=zh_CN";
        userInfoUrl = StrUtil.format(userInfoUrl, token, openid);
        res = HttpUtil.get(userInfoUrl);
        JSONObject userInfoJson = handleResult(res);
        return JSONObject.parseObject(userInfoJson.toJSONString(), WxUser.class);
    }

    private JSONObject handleResult(String str) {
        JSONObject result = JSONObject.parseObject(str);
        if (StrUtil.isNotBlank(result.getString("errcode"))) {
            if (result.getString("errcode").equals("0")) {
                return result;
            }
            throw new RuntimeException(StrUtil.format("微信接口出错, errcode: {}, msg: {}"
                    , result.getString("errcode"), result.getString("errmsg")));
        }
        return result;
    }
}
@Data
@ApiModel(value = "WxUser", description = "微信用户信息")
public class WxUser {
    @ApiModelProperty(value = "用户的唯一标识")
    private String openid;
    @ApiModelProperty(value = "用户昵称")
    private String nickname;
    @ApiModelProperty(value = "用户的性别,值为1时是男性,值为2时是女性,值为0时是未知")
    private Integer sex;
    @ApiModelProperty(value = "用户个人资料填写的省份")
    private String province;
    @ApiModelProperty(value = "普通用户个人资料填写的城市")
    private String city;
    @ApiModelProperty(value = "国家,如中国为CN")
    private String country;
    @ApiModelProperty(value = "用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效")
    private String headimgurl;
    @ApiModelProperty(value = "用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)")
    private String privilege;
    @ApiModelProperty(value = "只有在用户将公众号绑定到微信开放平台账号后,才会出现该字段")
    private String unionid;

}

  • 27
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值