微信开发官网:
https://open.weixin.qq.com
根据步骤申请成功后会返回应用注册数据,用于微信接口请求
wx:
appid: 应用名id
appsecret: 应用名密匙
参考文档:
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=e547653f995d8f402704d5cb2945177dc8aa4e7e&lang=zh_CN
https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
public R getLoginParam(HttpSession session) throws UnsupportedEncodingException {
Map<String,Object> map = new HashMap<>();
String url = URLEncoder.encode(wxConfig.getRedirecturl(), "UTF-8");
map.put("redirectUri",url);
map.put("scope","snsapi_login");
map.put("appid",wxConfig.getAppid());
map.put("state",System.currentTimeMillis()+"");
return R.ok().data(map);
}
参数标准:
redirect_uri?code=CODE&state=STATE
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
StringBuilder baseAccessTokenUrl = new StringBuilder();
baseAccessTokenUrl.append("https://api.weixin.qq.com/sns/oauth2/access_token")
.append("?appid=%s").append("&secret=%s").append("&code=%s").append("&grant_type=authorization_code");
String url = String.format(baseAccessTokenUrl.toString(), wxConfig.getAppid(), wxConfig.getAppsecret(), code);
try {
//通过请求微信接口携带code和oppid得到openid和access_token(接口调用凭证)
String jsonString = HttpClientUtils.get(url);
JSONObject jsonObject = JSONObject.parseObject(jsonString);
String openid = (String) jsonObject.get("openid");
String access_token = (String) jsonObject.get("access_token");
{
“access_token”:“ACCESS_TOKEN”, “expires_in”:7200,
“refresh_token”:“REFRESH_TOKEN”, “openid”:“OPENID”, “scope”:“SCOPE”,
“unionid”: “o6_bmasdasdsad6_2sgVt7hMZOPfL”
}
获取access_token后,进行接口调用,有以下前提:
- access_token有效且未超时;
- 微信用户已授权给第三方应用帐号相应接口作用域(scope)。
调用接口请求路径示例
http请求方式: GET
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
//通过openid和access_token调用微信接口获取用户信息
String userInfoStr = HttpClientUtils.get("https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid);
JSONObject userInfo = JSONObject.parseObject(userInfoStr);
//解析用户信息
String nickname = (String) userInfo.get("nickname");//用户号昵称
String headimgurl = (String) userInfo.get("headimgurl");//用户头像地址连接
返回说明
正确的Json返回结果:
{
“openid”:“OPENID”, “nickname”:“NICKNAME”, “sex”:1,
“province”:“PROVINCE”, “city”:“CITY”, “country”:“COUNTRY”,
“headimgurl”:
“https://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0”,
“privilege”:[ “PRIVILEGE1”, “PRIVILEGE2” ], “unionid”: "
o6_bmasdasdsad6_2sgVt7hMZOPfL"}
@ApiOperation("微信回调函数")
@GetMapping("/callback")
public String callback(String code,String state,HttpSession session){
StringBuilder baseAccessTokenUrl = new StringBuilder();
baseAccessTokenUrl.append("https://api.weixin.qq.com/sns/oauth2/access_token")
.append("?appid=%s").append("&secret=%s").append("&code=%s").append("&grant_type=authorization_code");
String url = String.format(baseAccessTokenUrl.toString(), wxConfig.getAppid(), wxConfig.getAppsecret(), code);
//通过请求微信接口携带code和oppid得到openid和access_token(接口调用凭证)
String jsonString = HttpClientUtils.get(url);
JSONObject jsonObject = JSONObject.parseObject(jsonString);
String openid = (String) jsonObject.get("openid");
String access_token = (String) jsonObject.get("access_token");
//通过openid和access_token调用微信接口获取用户信息
String userInfoStr = HttpClientUtils.get("https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid);
JSONObject userInfo = JSONObject.parseObject(userInfoStr);
//解析用户信息
String nickname = (String) userInfo.get("nickname");//用户号昵称
String headimgurl = (String) userInfo.get("headimgurl");//用户头像地址连接
}
(1)先根据appid和appsecret封装参数由前端生成请求登录二维码,供用户扫描登录,(2)用户授权登录后触发回调函数,携带返回参数code,根据code,封装请求参数,请求微信接口,换access_token(接口调用凭证)(3)最后根据access_token调用微信接口换取用户信息