微信开发之网页授权获取用户信息
2017-07-13 16:07
597 查看
注意:开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名:
回调域名中的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;
}
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;
}
}
定义一个实体类:将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"));
}
运行测试类后,将获取到的链接放进微信浏览器点击
回调域名中的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.openConnectio 8b17 n(); 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"));
}
运行测试类后,将获取到的链接放进微信浏览器点击
相关文章推荐
- 微信开发笔记——微信网页登录授权,获取用户信息
- 夺命雷公狗---微信开发51----网页授权(oauth2.0)获取用户基本信息接口(1)
- 微信开发--网页授权获取用户基本信息
- 微信开发-网页授权获取用户基本信息
- 夺命雷公狗---微信开发53----网页授权(oauth2.0)获取用户基本信息接口(3)实现世界留言版
- 微信网页开发授权(获取用户信息) 微信公众平台开发教程(4)
- 微信开发——网页授权获取用户基本信息
- 微信开发之获取OAuth2.0网页授权认证和获取用户信息进行关联
- 微信接口开发之高级篇系列【网页授权获取用户基本信息】
- 微信开发之网页授权获取用户信息(二)
- 夺命雷公狗---微信开发52----网页授权(oauth2.0)获取用户基本信息接口(2)
- 微信开发笔记——微信网页登录授权,获取用户信息
- 微信公众号开发-微信网页授权获取用户openid以及用户信息的步骤
- 微信网页开发之网页授权获取用户信息
- 微信公众号开发-微信网页授权获取用户openid以及用户信息的步骤
- 微信开发 网页授权获取用户基本信息
- 微信开发之网页授权获取用户信息(二)
- nodejs微信开发--网页获取用户授权信息
- 微信公众号开发二:关于授权网页获取用户微信信息注册用户的开发
- 微信开发-网页授权获取用户信息