微信 获取 用户 昵称
Posted sorta
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信 获取 用户 昵称相关的知识,希望对你有一定的参考价值。
import java.util.ResourceBundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
public class WxUserInfo {
private static Logger log = LoggerFactory.getLogger(WxUserInfo.class);
private static ResourceBundle wx = ResourceBundle.getBundle("wx");// 读取配置文件
private static final String TOKEN = wx.getString("Token");
private static final String APPID = wx.getString("appID");
private static final String APPSECRET = wx.getString("appsecret");
private static final String ACCESS_TOKEN_URL = wx.getString("ACCESS_TOKEN_URL");
private static final String WEIXIN_USERINFO_URL = wx.getString("WEIXIN_USERINFO_URL");
/**
* 获取微信用户信息
* (需scope为 snsapi_userinfo)
* @param code
* @return
*/
public static String getWxUserInfo(String code){
log.info("=== 根据code < "+code+" > 获取 用户唯一标识 openid ===");
String wxUserInfo ="{}";
if("".equals(code) || code == null) {
log.info("=== code 不存在 ===");
return wxUserInfo;
}else{
String accessTokenUrl = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("SECRET", APPSECRET).replace("CODE", code);
String accessTokenString = SendQuest.sendGet(accessTokenUrl);
JSONObject accessTokenJson = JSONObject.parseObject(accessTokenString);//{"errcode":40029,"errmsg":"invalid code"}
if (!accessTokenJson.containsKey("errcode")) {
log.info("=== 根据code < "+code+" > 获取 用户唯一标识 openid success===");
// int expires_in = accessTokenJson.getInteger("expires_in");//错误状态码
String access_token = accessTokenJson.getString("access_token");
String openid = accessTokenJson.getString("openid");
log.info("=== 获取 access_token < "+access_token+" > openid < " +openid+" > 成功===");
String weixinUserInfoUrl = WEIXIN_USERINFO_URL.replace("ACCESS_TOKEN", access_token).replace("OPENID", openid);
String weixinUserInfoString = SendQuest.sendGet(weixinUserInfoUrl);
JSONObject weixinUserInfoJson = JSONObject.parseObject(weixinUserInfoString);//{"errcode":40029,"errmsg":"invalid code"}
if (!weixinUserInfoJson.containsKey("errcode")) {
log.info("=== 获取 wxUserInfo 成功===");
log.info(weixinUserInfoString);
// String nickname = accessTokenJson.getString("nickname");
return weixinUserInfoString;
}else{
log.info("=== 获取 WxuserInfo 失败===");
log.info(weixinUserInfoString);
return weixinUserInfoString;
}
}else{
log.info("=== 获取 access_token < > openid < > 失败===");
log.info(accessTokenString);
return accessTokenString;
}
}
}
}
以上是关于微信 获取 用户 昵称的主要内容,如果未能解决你的问题,请参考以下文章