java实现微信授权获取用户openid及授权用户相关信息
首先需要在微信公众平台申请账号并在微信公众平台->设置->公众号设置->功能设置中设置或者在平台下接口权限->网页权限->网页授权中设置授权域名。跳转到授权页面的url:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
需要问自己拼接url,其中redirect_url为授权域名下的连接地址,用于获取code并进行获取openid的后台实现,redirect地址需要进行urlecode,scope中参数:snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
跳转页面中的处理代码:
@Namespace("/weixinInfo")
@Results({@Result(name = "ok", location = "/index.jsp"),})
public class GetUserOpenAction extends OutInterfaceAction{
private static final long serialVersionUID = 1L;
@Action("getCode")
public String getCode() throws ClientProtocolException, IOException{
String code = request.getParameter("code");
System.out.println(code);
if (code != null) {
//获取openid和access_token的连接
String getOpenIdUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code";
//获取返回的code
String requestUrl = getOpenIdUrl.replace("CODE", code);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(requestUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
//向微信发送请求并获取response
String response = httpClient.execute(httpGet,responseHandler);
System.out.println("=========================获取token===================");
System.out.println(response);
JsonParser parser = new JsonParser();
JsonObject jsonObject = (JsonObject) parser.parse(response);
String access_token = jsonObject.get("access_token").getAsString();
String openId = jsonObject.get("openid").getAsString();
System.out.println("=======================用户access_token==============");
System.out.println(access_token);
System.out.println(openId);
//获取用户基本信息的连接
String getUserInfo = "https://api.weixin.qq.com/sns/userinfo? access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
String userInfoUrl = getUserInfo.replace("ACCESS_TOKEN", access_token).replace("OPENID", openId);
HttpGet httpGetUserInfo = new HttpGet(userInfoUrl);
String userInfo = httpClient.execute(httpGetUserInfo,responseHandler);
//微信那边采用的编码方式为ISO8859-1所以需要转化
String json = new String(userInfo.getBytes("ISO-8859-1"),"UTF-8");
System.out.println("====================userInfo==============================");
JsonObject jsonObject1 = (JsonObject) parser.parse(json);
String nickname = jsonObject1.get("nickname").getAsString();
String city = jsonObject1.get("city").getAsString();
String province = jsonObject1.get("province").getAsString();
String country = jsonObject1.get("country").getAsString();
String headimgurl = jsonObject1.get("headimgurl").getAsString();
//性别 1 男 2 女 0 未知
Integer sex = jsonObject1.get("sex").getAsInt();
System.out.println("昵称"+nickname);
System.out.println("城市"+city);
System.out.println("省"+province);
System.out.println("国家"+country);
System.out.println("头像"+headimgurl);
System.out.println("性别"+sex);
System.out.println(userInfo);
}
return "ok";
}
}
后台打印的结果:
由于是测试代码,没有对两次的返回参数进行封装,可以对参数封装进行相应的存储
微信官网的文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
最后,推广一个阿里云服务器的优惠连接(https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=dq75ajpx)近期阿里一直做活动,有需要云服务器的可以点击连接领取优惠券。