微信開發(二)——獲取微信使用者的基本資訊
阿新 • • 發佈:2018-12-18
我們在微信開發過程中,很可能需要獲取授權使用者的微信頭像來作為系統的預設頭像,這是個非常簡單的實現 現在我們的手上已經用於openid和accesstoken(這不是全域性token!!!),不知道如何獲取的見上一節,微信開發獲取openid
我們通過以下方法
/** * 獲取使用者的資訊 * @param openId * @param accessToken * @return */ public static JSONObject getUserInfo(String openId, String accessToken) { StringBuffer userInfoUrl = new StringBuffer("https://api.weixin.qq.com/sns/userinfo?access_token=") .append(accessToken) .append("&openid=").append(openId) .append("&lang=zh_CN"); String userInfoStr = HttpClientHelper.getHtml(userInfoUrl.toString(), "UTF-8", 5); JSONObject jo = JSONObject.parseObject(userInfoStr); return jo; }
通過拼接字串傳送get請求,返回的json中就包含使用者的基本資訊了。
String image = userJson.getString("headimgurl");