微信测试号申请地址:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login;
微信公众平台开发者文档地址:https://mp.weixin.qq.com/wiki/home/index.html;
网页授权获取用户基本信息
具体步骤:
- 1 第一步:用户同意授权,获取code
- 2 第二步:通过code换取网页授权access_token
- 3 第三步:刷新access_token(如果需要)
- 4 第四步:拉取用户信息(需scope为 snsapi_userinfo)
1.用户同意授权,获取code
if(openId == null || openId.equals("")){
//get userInfo
//第一步:用户同意授权,获取code
String code = (String)request.getParameter("code");
System.out.println("code:"+code);
if(null == code || code.equals("")){
String GetCodeRequest = WxConfig.WEIXIN_OPENID_URL;
String result = null;
String uri = basePath + "lottery/toLottery.action?lottery.id=" + this.lottery.getId();
System.out.println("rui:"+uri);
if(flag == null){
flag="game";
}
try{
GetCodeRequest = GetCodeRequest.replace("APPID", URLEncoder.encode(serviceConf.getAppId(),"UTF-8"));
GetCodeRequest = GetCodeRequest.replace("REDIRECT_URI",URLEncoder.encode(uri,"UTF-8"));
GetCodeRequest = GetCodeRequest.replace("SCOPE", URLEncoder.encode(WxConfig.WEIXIN_SCOPE,"UTF-8"));
GetCodeRequest = GetCodeRequest.replace("STATE", URLEncoder.encode(flag,"UTF-8"));
result = GetCodeRequest;
System.out.println("result:"+result);
request.setAttribute("result", result);
return "getOpenId";
}catch(Exception e){
e.printStackTrace();
logger.error("get code error"+e);
}
}else{
openId = WxConfig.getOpenId(code,serviceConf.getAppId(),serviceConf.getAppSecret());
request.getSession().setAttribute("openId", openId);
logger.info("get openId:"+openId);
}WxConfig.java代码
public final static String WEIXIN_OPENID_URL ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
//根据access_token获取openid
public final static String WEIXIN_ACCESS_TOKEN_OPENID_URL ="/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
<pre name="code" class="java">//获取用户标识openid
public static String getOpenId(String code,String appId,String appSecret){
String GetOpenidRequest = HttpUtil.WEIXIN_HOST_API +WEIXIN_ACCESS_TOKEN_OPENID_URL;
logger.info("getOpenId微信提交路径:"+GetOpenidRequest);
GetOpenidRequest = GetOpenidRequest.replace("APPID", appId);
GetOpenidRequest = GetOpenidRequest.replace("SECRET", appSecret);
GetOpenidRequest = GetOpenidRequest.replace("CODE", code);
logger.info("getOpenId微信提交路径:"+WEIXIN_ACCESS_TOKEN_OPENID_URL);
JSONObject json = JSONObject.fromObject(HttpUtil.get(GetOpenidRequest));
System.out.println(json);
String openid;
try{
logger.info("获取openid--getOpenId()");
openid = json.getString("openid");
String accessToken = json.getString("access_token");
logger.info("微信获取到的用户标识openid:"+openid+";access_token:"+accessToken);
return openid;
}catch (JSONException e) {
openid = null;
// 获取openid失败
e.printStackTrace();
logger.error("获取token失败 errcode:{}:"+json.getInt("errcode")+"errmsg:{}:"+json.getString("errmsg"));
}
return openid;
}
本文介绍了使用Java进行微信网页授权以获取用户基本信息的流程,包括四个步骤:用户同意授权获取code、通过code换取access_token、可能的access_token刷新及使用snsapi_userinfo获取用户详细信息。

2万+

被折叠的 条评论
为什么被折叠?



