微信高級接口獲取用戶基本信息

微信高級接口獲取用戶基本信息


首先在微信裏配置的就不說了

先來看這個鏈接

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章