今天写一下微信开发获取openId以及用户信息,后续抽时间再写一下,微信的bug调试环境搭建。
不多说直接上java代码!
关于接口里需要的code如何生成 大家可以借鉴这位大佬的博客:https://www.jianshu.com/p/b5929770f92d
package com.example.demo.wx;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class WeChatAuth {
//网页授权获取access_token
public final static String OAUTH_URL_OPENID = "https://api.weixin.qq.com/sns/oauth2/access_token";
//网页授权获取用户信息
public final static String OAUTH_URL_USERINFO = "https://api.weixin.qq.com/sns/userinfo";
/**
* 获取微信信息
*
* @param code 前端获取的微信code
* @param appId 微信appid
* @param appSecret 微信appSecret
* @return
*/
public static Object indexUserInfo(String code, String appId, String appSecret) {
String result = null;
//获取openid、accessToken
Map wxInfo = findAccessToken(appId, appSecret, code);
String openid = null;
String accessToken = null;
if (wxInfo != null) {
if (wxInfo.get("openid") != null) {
openid = wxInfo.get("openid").toString();
}
if (wxInfo.get("accessToken") != null) {
accessToken = wxInfo.get("accessToken").toString();
}
}
//通过accessToken、openid获取微信用户详细信息
Customer authCustomer = findWxUserInfo(accessToken, openid);
if (authCustomer == null) {
System.out.println("微信用户oauth2.0用户信息获得失败:accessToken:" + accessToken + ";openid" + openid);
return result;
}
return authCustomer;
}
/**
* 获取openId、accessToken
*
* @param appId
* @param appSecret
* @param code
* @return
*/
public static Map findAccessToken(String appId, String appSecret, String code) {
Map result = new HashMap();
try {
HttpResponse<JsonNode> jsonResponse = Unirest
.post(OAUTH_URL_OPENID)
.queryString("appid", appId)
.queryString("secret", appSecret)
.queryString("code", code)
.queryString("grant_type", "authorization_code")
.asJson();
JSONObject jb = jsonResponse.getBody().getObject();
if (!jb.isNull("openid") && jb.get("openid") != null) {
String openid = jb.get("openid").toString();
result.put("openid", openid);
}
if (!jb.isNull("access_token") && jb.get("access_token") != null) {
String accessToken = jb.get("access_token").toString();
result.put("accessToken", accessToken);
}
} catch (Exception e) {
System.out.println("接口获取用户信息报错,appid:" + appId + " secret:" + appSecret + " code:" + code);
}
return result;
}
/**
* 获取微信用户信息
*
* @param accessToken
* @param openid
* @return
*/
public static Customer findWxUserInfo(String accessToken, String openid) {
Customer customer = new Customer();
try {
HttpResponse<JsonNode> jsonResponse = Unirest
.post(OAUTH_URL_USERINFO)
.queryString("access_token", accessToken)
.queryString("openid", openid)
.queryString("lang", "zh_CN")
.asJson();
JSONObject jb = jsonResponse.getBody().getObject();
if (!jb.isNull("openid") && jb.get("openid") != null) {
customer.setOpenid(jb.get("openid").toString());
}
if (!jb.isNull("nickname") && jb.get("nickname") != null) {
customer.setNickname(jb.get("nickname").toString());
}
if (!jb.isNull("sex") && jb.get("sex") != null) {
customer.setSex(Integer.parseInt(jb.get("sex").toString()));
}
if (!jb.isNull("province") && jb.get("province") != null) {
customer.setProvince(jb.get("province").toString());
}
if (!jb.isNull("city") && jb.get("city") != null) {
customer.setCity(jb.get("city").toString());
}
if (!jb.isNull("country") && jb.get("country") != null) {
customer.setCountry(jb.get("country").toString());
}
if (!jb.isNull("headimgurl") && jb.get("headimgurl") != null) {
customer.setHeadImageUrl(jb.get("headimgurl").toString());
}
if (!jb.isNull("unionid") && jb.get("unionid") != null) {
customer.setUnionid(jb.get("unionid").toString());
}
} catch (Exception e) {
System.out.println("接口获取用户信息报错,accessToken:" + accessToken + " openid:" + openid);
}
return customer;
}
/**
* 测试main方法
*
* @param args
*/
public static void main(String[] args) {
indexUserInfo("微信返回的code", "你的appid", "你的secret");
}
}
实体类:
package com.example.demo.wx;
public class Customer implements java.io.Serializable {
private String openid;
//企业属性
private String unionid;
//微信昵称
private String nickname;
//性别
private Integer sex;
//省份
private String province;
//城市
private String city;
//国家
private String country;
//微信头像
private String headImageUrl;
/**
* default constructor
*/
public Customer() {
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public String getUnionid() {
return unionid;
}
public void setUnionid(String unionid) {
this.unionid = unionid;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getHeadImageUrl() {
return headImageUrl;
}
public void setHeadImageUrl(String headImageUrl) {
this.headImageUrl = headImageUrl;
}
}