微信小程序获取session_key,access_token,open_id方法

1、首先登录微信公众平台,拿到自己的appId,secret,这一步很重要

2、微信小程序客户端,使用wx.login(res)获取code,即res.code,通过wx.request请求到自己搭建的服务器

function requestUserInfo(userInfo){
  var that = this;
  wx.login({
    success(res){
      var code = res.code;
      console.log("code:",code)
      console.log("userInfo:",userInfo)
      wx.request({
        url: app.globalData.baseUrl + '/wxlogin',
        data: {
          code: code,
          name: userInfo.nickName,
          city: userInfo.city,
          country: userInfo.country,
          gender: userInfo.gender,
          province: userInfo.province
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'POST',
        success(res) {
          console.log(res.data)
        },
        fail(res){
          console.log(res.data)
        }
      })
    }
  })
}

3、通过appId,secret,code,访问请求地址

GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

 就可以获得session_key,open_id

 

4、通过appId,secret,访问请求地址

GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

 就可以获取得到access_token

5、代码如下,使用的hutool.jar访问上面的微信请求链接

private static String getOpenidURLFormat
            = "https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code";
    //通过 app_id, app_secret 获取接口调用凭证 session_key
    private static String getAccessTokenURLFormat
            = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}";
    /**
     * 获取AccessToken
     */
    public static JSONObject getAccessToken(){
        //通过hutool工具对占位符{}进行填充
        String getAccessTokenURL = StrUtil.format(getAccessTokenURLFormat,app_id,app_secret);
        //通过hutool工具问url
        String tokenResponse = HttpUtil.get(getAccessTokenURL);
        //通过hutool工具类转换为json
        JSONObject tokenJson = JSONUtil.parseObj(tokenResponse);

        String access_token = (String) tokenJson.get("access_token");
        Integer expires_in = (Integer) tokenJson.get("expires_in");
        Integer errcode = (Integer) tokenJson.get("errcode");
        String errmsg = (String) tokenJson.get("errmsg");

        System.out.println("接口调用 access_token:"+ access_token);

        return tokenJson;
    }

    /**
     * 获取openId和SessionKey
     */
    public static JSONObject getOpenidAndSessionKey(String code){
        //通过hutool工具对占位符{}进行填充
        String getOpenidURL = StrUtil.format(getOpenidURLFormat,app_id,app_secret,code);
        //通过hutool工具类访问url
        String openidResponse = HttpUtil.get(getOpenidURL);
        //通过hutool工具类转换为json
        JSONObject openidJson = JSONUtil.parseObj(openidResponse);
        /**
         * 获取json字段errCode,如果errCode!=null则登录失败
         */
        Integer errcode = (Integer) openidJson.get("errcode");
        String errmsg = (String)openidJson.get("errmsg");
        String openid = (String)openidJson.get("openid");
        String session_key = (String)openidJson.get("session_key");

        System.out.println("授权临时票据 code:"+code);
        System.out.println("会话密钥 access_token:"+session_key);
        System.out.println("授权用户唯一标识 openid:"+openid);

        return openidJson;
    }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值