微信高级接口获取用户基本信息
首先在微信里配置的就不说了
先来看这个链接
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2176067bd79f4e14
&redirect_uri=http://hanxiaozhuan.oicp.net/webChat/oauth2/getCodeAndUserId //这个是请求后回调地址
&response_type=code
&scope=snsapi_userinfo
&state=1
#wechat_redirect
请求完这个链接 会返回到回调地址里 并带上参数 code state
通过code 请求到
access_token <pre name="code" class="java">openid
然后 用这两个 请求到 用户基本信息
看代码
<span style="white-space:pre"> </span>String get_access_token_url="https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid=APPID" +
"&secret=SECRET&" +
"code=CODE&grant_type=authorization_code";
String get_userinfo="https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String code=request.getParameter("code");
if (!"authdeny".equals(code)) {
get_access_token_url=get_access_token_url.replace("APPID", "公众号appid");
get_access_token_url=get_access_token_url.replace("SECRET", "公众号secret");
get_access_token_url=get_access_token_url.replace("CODE", code);
String json=HttpUtil.getUrl(get_access_token_url);
JSONObject jsonObject=JSONObject.fromObject(json);
String access_token=jsonObject.getString("access_token");
String openid=jsonObject.getString("openid");
get_userinfo=get_userinfo.replace("ACCESS_TOKEN", access_token);
get_userinfo=get_userinfo.replace("OPENID", openid);
String userInfoJson=HttpUtil.getUrl(get_userinfo);
JSONObject userInfoJO=JSONObject.fromObject(userInfoJson);
String user_openid=userInfoJO.getString("openid");
String user_nickname=userInfoJO.getString("nickname");
String user_headimgurl=userInfoJO.getString("headimgurl");
<span style="white-space:pre"> </span>}
源码下载:http://download.csdn.net/detail/laigezao/8974235