@Value("${wx.open.app_id}")
private String APP_ID;
@Value("${wx.open.app_secret}")
private String APP_SECRET;
public String getOpenId(String code) {
String accessToken;
try {
String getToken = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid=%s" +
"&secret=%s" +
"&code=%s" +
"&grant_type=authorization_code";
getToken = String.format(getToken,
APP_ID,
APP_SECRET,
code);
accessToken = HttpClientUtil.get(getToken);
} catch (Exception e) {
e.printStackTrace();
}
Map map = JSON.parseObject(accessToken);
String openid = (String) map.get("openid");
//todo 开始后续的操作
return openid;
}
每个code只能使用一次