微信公众号开发(四)
获取用户信息
- 第一步
引导关注者打开如下页面:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
来获取code, 如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE
- 第二步
获取code后,请求以下链接获取access_token和opendi:
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
正确时返回的JSON数据包如下:
{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}
- 第三步
获取第二步的refresh_token后,请求以下链接获取access_token:
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
- 第四步
拉取用户信息(需scope为 snsapi_userinfo)
请求http:GET(请使用https协议)
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
正确时返回的JSON数据包如下:
{
"openid":" OPENID",
" nickname": NICKNAME,
"sex":"1",
"province":"PROVINCE"
"city":"CITY",
"country":"COUNTRY",
"headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[
"PRIVILEGE1"
"PRIVILEGE2"
],
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
正式开发
在开发前首先需要在公众平台里设置网页授权信息
首先我们这里是用springmvc的,这里就不多说了,这里编写了一个userAction,我们在微信的自定义菜单添加一个按钮指向这里的getWxUserMsg方法,自定义菜单的设置在之前已经说过了。
package com.feng.web.controller;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.feng.web.model.WebAccessToken;
import com.feng.web.model.WxConfig;
import com.feng.web.model.WxUser;
import com.feng.web.service.util.GsonUtils;
import com.feng.web.service.util.NetUtil;
@Controller
@RequestMapping(value="/user")
public class UserAction {
@RequestMapping(value="getWxUserMsg", method=RequestMethod.GET)
public String getWxUserMsg(Model model, String code){
System.out.println("useraction: "+code);
String url = String.format(WxConfig.GET_WEBTOKEN, "wxa1d7918774ce0f09", "8cfb267d462eae71fb365eb9e35dc201", code);
String resp = NetUtil.httpGet(url);
WebAccessToken webAccessToken = GsonUtils.json2Bean(resp, WebAccessToken.class);
if(webAccessToken != null){
url = String.format(WxConfig.GET_USERMSG, webAccessToken.getAccess_token(), webAccessToken.getOpenid());
resp = NetUtil.httpGet(url);
model.addAttribute("user", GsonUtils.json2Bean(resp, WxUser.class));
}
return "user/wsusermsg";
}
}
WebAccessToken
package com.feng.web.model;
public class WebAccessToken {
private String access_token;
private int expires_in;
private String refresh_token;
private String openid;
private String scope;
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public int getExpires_in() {
return expires_in;
}
public void setExpires_in(int expires_in) {
this.expires_in = expires_in;
}
public String getRefresh_token() {
return refresh_token;
}
public void setRefresh_token(String refresh_token) {
this.refresh_token = refresh_token;
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
@Override
public String toString() {
return "WebAccessToken [access_token=" + access_token + ", expires_in="
+ expires_in + ", refresh_token=" + refresh_token + ", openid="
+ openid + ", scope=" + scope + "]";
}
}
WxConfig
package com.feng.web.model;
public class WxConfig {
//http请求方式: POST/FORM,需使用https
public static final String UPLOAD_MEDIA = "https://api.weixin.qq.com/cgi-bin/media/upload";
//http请求方式: GET,https调用
public static final String DOWNLOAD_MEDIA = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s";
//获取网页授权token
public static final String GET_WEBTOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
//获取用户信息
public static final String GET_USERMSG = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";
}
获取完会跳转到wsusermsg.jsp, 这里就简单地显示用户的信息
<body>
openid: ${user.openid} <br/>
nickname: ${user.nickname} <br/>
sex: ${user.sex} <br/>
province: ${user.province} <br/>
city: ${user.city} <br/>
country: ${user.country} <br/>
headimgurl: ${user.headimgurl} <br/>
<img src="${user.headimgurl}" /> <br/>
unionid: ${user.unionid} <br/>
</body>
调试结果: