1. 程式人生 > >java微信公眾號開發獲取微信使用者資訊

java微信公眾號開發獲取微信使用者資訊

此博文參考:https://www.cnblogs.com/sxmny/articles/4969119.html

獲取微信使用者的基礎資訊(包括頭像,暱稱,所在地等)大體分為如下三步:

1.獲取code;

只有使用者同意授權才能拿到使用者的微信資訊.使用者同意授權後,我們就可以拿到code值.

code是怎麼來的,是通過呼叫下面介面來獲取的:

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

注意這個介面中有個引數scope 預設有2個值snsapi_base和snsapi_userinfo,這個介面會根據scope 來生成不同的code,並且獲取不同作用的access_token ,不管scope傳什麼值都能在得到對應access_token的同時得到open_id, 如果你只需要得到opend_id 那使用snsapi_base引數到此結束了,如果需要獲取使用者的其他資訊比如 暱稱 地址 就要snsapi_userinfo 會彈出授權

2.獲取access_token和openid

 使用者網頁授權access_token 獲取介面地址是

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

  網頁授權access_token 需要通過code去獲取,此code即為之前獲取到的(code一般由前臺根據使用者是否同意授權獲取到,然後傳遞給後臺,後臺直接用就可以).

 3 .獲取使用者資訊

  https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN

 access_token即為第二部獲取的值

注意:通過以上三步你可以獲取到一個微信使用者的基本資訊,但是並不能獲取到公眾號關注者的微信使用者資訊.想要獲取到微信公眾號關注者的微信資訊,也需要上述三步,只是獲取access_token和獲取使用者的介面有區別.如下:

前提條件:1.使用者同意授權公眾號獲取其使用者資訊;2:使用者關注了此公眾號;

獲取使用者資訊介面:

https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN  (此介面的access_token 是呼叫基礎介面獲取到的access_token, 不是網頁授權獲取到的access_token(上述第2步獲取到的),直白點說,就是下面這個介面獲取到的access_token)

基礎介面獲取access_token, 獲取介面如下:  

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

針對公眾號獲取微信使用者的資訊,微信的解釋:是在使用者和公眾號產生訊息互動或關注後事件推送後,才能根據使用者OpenID來獲取使用者基本資訊。這個介面,包括其他微信介面,都是需要該使用者(即openid)關注了公眾號後,才能呼叫成功的。

此博文上述內容參考:https://www.cnblogs.com/sxmny/articles/4969119.html

程式碼如下:

/**
 * 獲取微信使用者的openId
 * @param appId
 * @param code
 * @param secret
 * @return
 */
public Map getOpenId(String appId, String code, String secret){
    Map map = new HashMap();
    String resultUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+ appId +"&secret="+ secret +"&code="+ code +"&grant_type=authorization_code";
    // 獲取openid
    String json = HttpUtil.get(resultUrl);
    JSONObject jsonObject = JacksonUtil.toEntity(json, JSONObject.class);
    Object openid1 = jsonObject.get("openid");
    String openid="";
    if (null != openid1){
        openid = openid1.toString();
    }
    map.put("openid",openid);
    return map;
}
/**
 * 獲取微信使用者資訊
 * @param code
 * @return
 */
public Map getWXInfo(String code, String appId,String secret) {
    if (StrUtil.isNotBlank(code)){
        Map tokenMap = getOpenId(appId, code, secret);
        String openid = tokenMap.get("openid").toString();
        if (StrUtil.isNotBlank(openid)){
            // 獲取基礎支援的access_token
            String resultUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId +"&secret="+ secret;
            String json = HttpUtil.get(resultUrl);
            JSONObject jsonObject = JacksonUtil.toEntity(json, JSONObject.class);
            String access_token = jsonObject.get("access_token").toString();
            // 獲取使用者資訊
            String resultUrl2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+ access_token +"&openid="+ openid +"&lang=zh_CN";
            String json2 = HttpUtil.get(resultUrl2);
            JSONObject jsonObject2 = JacksonUtil.toEntity(json2, JSONObject.class);
            String nickname = jsonObject2.get("nickname").toString();
            String headimgurl = jsonObject2.get("headimgurl").toString();
            if (nickname == null || headimgurl == null){
                throw new BadRequestException("未關注,請關注後重試!");
            }
            String sex = jsonObject2.get("sex").toString();
            String country = jsonObject2.get("country").toString();
            String province = jsonObject2.get("province").toString();
            String city = jsonObject2.get("city").toString();
            HashMap<String,Object> map = new HashMap<>();
            map.put("nickName",nickname);
            map.put("country",country);
            map.put("provinceName",province);
            map.put("cityName",city);
            map.put("headImg",headimgurl);
            map.put("openId",openid);
            map.put("sex",sex);
            return map;
        }
    }
    return null;
}