微信 网页授权获取用户基本信息 (一)

程序实现如下

第一步:用户同意授权,获取code

1、以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)

2、以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。

	public String getCode(HttpServletRequest request){	
		String redirect_uri=loadurl;
//		String oauth2Url =
//				"https://open.weixin.qq.com/connect/oauth2/authorize?appid="+Constants.appid+"&redirect_uri="+redirect_uri+
//				"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect ";
		String oauth2Url =
				"https://open.weixin.qq.com/connect/oauth2/authorize?appid="+Constants.appid+"&redirect_uri="+redirect_uri+
				"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect ";
		return "redirect:" + oauth2Url;
	}


code说明 :
code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。

第二步:通过code换取网页授权access_token


public JSONObject getHtmlAccessToken(HttpServletRequest request){
		String code=request.getParameter("code");
		JSONObject	jsonObject =WXMethodUtil.html_access("你的appid", "你的secret", code);
    return jsonObject;
}

//WXMethodUtil.html_access 的方法
	public static JSONObject  html_access(String appid,String secret,String code){
		String access_token_url=" https://api.weixin.qq.com/sns/oauth2/access_token?appid="+"你的appid"+"&secret="+"你的secret"+"&code="+code+"&grant_type=authorization_code";
		JSONObject	jsonObject = HttpClientUtil.httpRequest(access_token_url, EnumMethod.GET.name(), null);
		return jsonObject;
	}



/***
 * 
 * @author yuki_ho
 *
 */
public class WXMethodUtil {
	private static final Logger logger = Logger.getLogger(WXMethodUtil.class);
	
	
	
	public static JSONObject  html_access(String appid,String secret,String code){
		String access_token_url=" https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
		JSONObject	jsonObject = HttpClientUtil.httpRequest(access_token_url, EnumMethod.GET.name(), null);
		return jsonObject;
	}
	
	public static JSONObject html_userInfo_url(String access_token,String openid){
		String userInfo_url="https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN ";
		JSONObject	jsonObject2 = HttpClientUtil.httpRequest(userInfo_url, EnumMethod.GET.name(), null);
		return jsonObject2;
	}
}



返回说明

正确时返回的JSON数据包如下:

{
   "access_token":"ACCESS_TOKEN",
   "expires_in":7200,
   "refresh_token":"REFRESH_TOKEN",
   "openid":"OPENID",
   "scope":"SCOPE"
}
 
第三步:拉去用户信息(需ocope为 snsapi_userinfo)
public JSONObject getWXuserInfo(JSONObject jsonObject){
    String access_token=jsonObject.get("access_token").toString();
	String openid=jsonObject.get("openid").toString();
    JSONObject jsonObject2 =WXMethodUtil.html_userInfo_url(access_token, openid);
    return jsonObject2;
}


返回说明

正确时返回的JSON数据包如下:

{
   "openid":" OPENID",
   " nickname": NICKNAME,
   "sex":"1",
   "province":"PROVINCE"
   "city":"CITY",
   "country":"COUNTRY",
    "headimgurl":    "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", 
	"privilege":[
	"PRIVILEGE1"
	"PRIVILEGE2"
    ]
}

帮助类:HttpClientUtil 连接
参考:连接

你可能感兴趣的:(微信-java,微信,java)