zoukankan      html  css  js  c++  java
  • 微信网页版扫一扫登录

    记得第一次做这个的时候看文档看的一篇茫然,不知怎么下手。其实根本不需要想那么多,先将代码敲起来,一步步梳理没有完不成的。

    具体的操作步骤在https://mp.weixin.qq.com/wiki/4/9ac2e7b1f1d22e9e57260f6553822520.html;

    网上相关的互相copy的实例有很多。最后其实发现跟着微信的公众平台开发者文档一步步来就行了;

    当然首先开发者需要先到公众平台官网中的开发者中心页配置授权回调域名,不然就没有下文了。我是根据公司的正义猫公众号(推销一波[嘿嘿嘿])来开发的。授权回调域名配置规范为全域名;

    好了重点说下具体步骤(附上官方步骤):

    对于第一步其实就是一个链接:"https://open.weixin.qq.com/connect/qrconnect?appid="+WeiXinAppId+ "&redirect_uri="+WX_CALLBACK_URL+ "&response_type=code&scope=snsapi_login&state=***#wechat_redirect"
    以上需要两个参数:WeiXinAppId就是公司正义猫公众号(继续推销[阴险脸.jpg])的appID,
    还有就是
    WX_CALLBACK_URL顾名思义就是回调地址了,这个你自己来定义回调地址的,你想回调到自己配置的controller层的哪个接口就是哪个,由你掌控开心就好。
    还是不懂就例如附上我的接口:

    @Hitlog
    @RequestMapping(value = "/weixin/callback", method = RequestMethod.GET)
    public String weixinCallback(HttpServletRequest request, HttpServletResponse response, Model model,
    @RequestParam(value = "state", defaultValue = "") String state,
    @RequestParam(value = "code", defaultValue = "") String code) {
    WxUser wxUser = oAuthService.loginByWeiXin(code);
    if (wxUser == null) {
    // 登录失败
    model.addAttribute("status", "fail");
    model.addAttribute("message", "授权登入失败");
    return "oauth/register";
    }
    UserOAuth uo = oAuthService.getWechatUserOAuthByOpenId(wxUser.getOpenId());

    if (uo == null) {
    model.addAttribute("status", "success");
    model.addAttribute("message", "用户请注册");
    model.addAttribute("username", wxUser.getNickname());
    model.addAttribute("avatar", wxUser.getHeadImgUrl());
    model.addAttribute("userOAuthType", UserOAuth.TYPE_WECAHT);
    model.addAttribute("accessToken", wxUser.getAccessToken());
    model.addAttribute("openId", wxUser.getOpenId());
    return "oauth/register";
    }else{
    oAuthService.updateWechatAccessToken(uo.getUserId(), wxUser.getAccessToken());
    User u = userService.getUserById(uo.getUserId());
    if (u != null) {
    doLogin(u.getId(), request, response);
    model.addAttribute("status", "success");
    model.addAttribute("message", "登录成功");
    return "oauth/register";
    }
    }

    return "oauth/register";
    }

    这里你可以获得两个参数state和code,重点是code,你可以根据code后端再去发送请求获取accesstoken。继续附上我的代码:

    public static WeixinOauth2Token getOauth2AccessToken(String appId,String appSecret,String code) {
    WeixinOauth2Token wat = null;
    // 拼接请求地址
    String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
    requestUrl = requestUrl.replace("APPID", appId);
    requestUrl = requestUrl.replace("SECRET", appSecret);
    requestUrl = requestUrl.replace("CODE", code);
    JSONObject jsonObject = httpsRequest(requestUrl, "GET", null);
    if (null != jsonObject) {
    try {
    wat = new WeixinOauth2Token();
    wat.setAccessToken(jsonObject.getString("access_token"));
    wat.setExpiresIn(jsonObject.getInt("expires_in"));
    wat.setRefreshToken(jsonObject.getString("refresh_token"));
    wat.setOpenId(jsonObject.getString("openid"));
    wat.setScope(jsonObject.getString("scope"));
    } catch (Exception e) {
    wat = null;
    int errorCode = 0;
    String errorMsg = null;
    try {
    errorCode = jsonObject.getInt("errcode");
    errorMsg = jsonObject.getString("errmsg");
    } catch (JSONException e1) {
    e1.printStackTrace();
    }
    log.error("获取网页授权凭证失败 errcode:{} errmsg:{}", errorCode, errorMsg);
    }
    }
    return wat;
    }

    再根据code获取用户的基本信息,继续参上代码:

    public WxUser loginByWeiXin(String code) {
    if(code==null){
    return null;
    }
    WeixinOauth2Token result = WxAuthorizeUtil.getOauth2AccessToken(WeiXinAppId,WeiXinAppSecret,code);
    if(result==null){
    return null;
    }
    WxUser wxUser = WxAuthorizeUtil.getWxUserInfo(result.getAccessToken(), result.getOpenId());
    if(null!=wxUser){
    wxUser.setAccessToken(result.getAccessToken());
    }
    return wxUser;
    }

    public static WeixinOauth2Token getOauth2AccessToken(String appId,String appSecret,String code) {
    WeixinOauth2Token wat = null;
    // 拼接请求地址
    String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=***&grant_type=authorization_code";
    requestUrl = requestUrl.replace("APPID", appId);
    requestUrl = requestUrl.replace("SECRET", appSecret);
    requestUrl = requestUrl.replace("CODE", code);
    JSONObject jsonObject = httpsRequest(requestUrl, "GET", null);
    if (null != jsonObject) {
    try {
    wat = new WeixinOauth2Token();
    wat.setAccessToken(jsonObject.getString("access_token"));
    wat.setExpiresIn(jsonObject.getInt("expires_in"));
    wat.setRefreshToken(jsonObject.getString("refresh_token"));
    wat.setOpenId(jsonObject.getString("openid"));
    wat.setScope(jsonObject.getString("scope"));
    } catch (Exception e) {
    wat = null;
    int errorCode = 0;
    String errorMsg = null;
    try {
    errorCode = jsonObject.getInt("errcode");
    errorMsg = jsonObject.getString("errmsg");
    } catch (JSONException e1) {
    e1.printStackTrace();
    }
    log.error("获取网页授权凭证失败 errcode:{} errmsg:{}", errorCode, errorMsg);
    }
    }
    return wat;
    }

    以上可以获取微信用户的头像、昵称、详细地址(如果用户自己微信账号有填写)、年龄、性别等等;

    关于封装类的参数:

    public class WeixinOauth2Token {
    // 网页授权接口调用凭证
    private String accessToken;
    // 凭证有效时长
    private int expiresIn;
    // 用于刷新凭证
    private String refreshToken;
    // 用户标识
    private String openId;
    // 用户授权作用域
    private String scope;}

    public class WxUser {

    // 用户标识
    private String openId;
    // 用户昵称
    private String nickname;
    // 性别(1是男性,2是女性,0是未知)
    private int sex;
    // 国家
    private String country;
    // 省份
    private String province;
    // 城市
    private String city;
    // 用户头像链接
    private String headImgUrl;
    // 用户特权信息
    private List<String> privilegeList;
    //微信用户accessToken
    private String accessToken;}

    其他的像Evernote、微博的授权登录步骤都相似,套路都是一样的。看完有帮助的童鞋留个言就好,轻喷(づ ̄3 ̄)づ╭❤~

  • 相关阅读:
    CSUFT 1002 Robot Navigation
    CSUFT 1003 All Your Base
    Uva 1599 最佳路径
    Uva 10129 单词
    欧拉回路
    Uva 10305 给任务排序
    uva 816 Abbott的复仇
    Uva 1103 古代象形文字
    Uva 10118 免费糖果
    Uva 725 除法
  • 原文地址:https://www.cnblogs.com/yzf666/p/6343490.html
Copyright © 2011-2022 走看看