微信高级接口获取用户基本信息

微信高级接口获取用户基本信息


首先在微信里配置的就不说了

先来看这个链接

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值