注意:开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名:
回调域名中的http://不需要添加。
详细参考微信开发文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
1.用户同意授权,获取code:方法封装在util类中
pojo:Code类
public class Code {
private String access_token = null;
private String expires_in = null;
private String refresh_token = null;
private String openid = null;
private String scope = null;
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public String getExpires_in() {
return expires_in;
}
public void setExpires_in(String expires_in) {
this.expires_in = expires_in;
}
public String getRefresh_token() {
return refresh_token;
}
public void setRefresh_token(String refresh_token) {
this.refresh_token = refresh_token;
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public static Code getS2(String code) {
Code returnCode = null;
InputStreamReader reader = null;
BufferedReader breader = null;
try {
URL url = new URL("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WX_ARG.appID+"&secret="+WX_ARG.appsecret+"&code="+code+"&grant_type=authorization_code");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
reader = new InputStreamReader(connection.getInputStream());
breader = new BufferedReader(reader);
String str = null;
StringBuffer strb = new StringBuffer();
while (null != (str = breader.readLine())) {
strb.append(str);
}
Gson gson = new Gson();
returnCode = gson.fromJson(strb.toString(), ReturnCode.class);
} catch (Exception e) {
e.printStackTrace();
}
try {
breader.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return returnCode;
}
2.义code换取accesstoken封装在util类中
accesstoken类:
public class AccessToken {
private String access_token = null;
private String expires_in = null;
/**
* @return the access_token
*/
public String getAccess_token() {
return access_token;
}
/**
* @param access_token the access_token to set
*/
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
/**
* @return the expires_in
*/
public String getExpires_in() {
return expires_in;
}
/**
* @param expires_in the expires_in to set
*/
public void setExpires_in(String expires_in) {
this.expires_in = expires_in;
}
}
public static AccessToken getAccessToken() {
AccessToken accessToken = null;
InputStreamReader reader = null;
BufferedReader breader = null;
try {
URL url = new URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WX_ARG.appID + "&secret=" + WX_ARG.appsecret);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
reader = new InputStreamReader(connection.getInputStream());
breader = new BufferedReader(reader);
String str = null;
StringBuffer strb = new StringBuffer();
while (null != (str = breader.readLine())) {
strb.append(str);
}
Gson gson = new Gson();
accessToken = gson.fromJson(strb.toString(), AccessToken.class);
} catch (Exception e) {
e.printStackTrace();
}
try {
breader.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return accessToken;
}
3.http请求封装在util类中:
public static String getS4(ReturnCode code) {
StringBuffer strb = new StringBuffer();
InputStreamReader reader = null;
BufferedReader breader = null;
try {
URL url = new URL("https://api.weixin.qq.com/sns/userinfo?access_token="+code.getAccess_token()+"&openid="+code.getOpenid()+"&lang=zh_CN");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
reader = new InputStreamReader(connection.getInputStream());
breader = new BufferedReader(reader);
String str = null;
while (null != (str = breader.readLine())) {
strb.append(str);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
breader.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return strb.toString();
}
定义一个实体类:将appid与appsecret值填充进去
测试:
public static String getS1(String redirect_uri, String state) {
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ WX_ARG.appID + "&redirect_uri=" + URLEncoder.encode(redirect_uri)
+ "&response_type=code&scope=snsapi_userinfo&state=" + state
+ "#wechat_redirect";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(WX_Util.getS1("http://fmsh.gq/wxx/index.jsp", "nfcostoken"));
}
运行测试类后,将获取到的链接放进微信浏览器点击