企业微信中第三方应用:用户授权登录
官方文档:https://work.weixin.qq.com/api/doc/90001/90142/90595
只做授权登录的话,直接看身份验证模块扫码授权登录就可以了
1。构建授权url
@ApiOperation("构造网页授权链接") @ApiImplicitParams({@ApiImplicitParam(name = "state", value = "用于企业或服务商自行校验session", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "redirectUri", value = "授权登录之后目的跳转网址", dataType = "string", required = true, paramType = "query")}) @GetMapping("/auth/qywxUrl") public Result makeUrl(@RequestParam(value = "state") String state,@RequestParam(value = "redirectUri", required = true)String redirectUri){ String qywxAuthUrl = thirdAuth.getQywxAuthUrl(corpID, redirectUri, state); return Result.success(qywxAuthUrl); } public String getQywxAuthUrl(String appid,String redirectUri, String state){ String qywxAuthUrl = ""; try { // redirect_uri需要经过一次urlencode作为参数 String redirectUri_encode = java.net.URLEncoder.encode(redirectUri, "UTF-8"); qywxAuthUrl = "https://open.work.weixin.qq.com/wwopen/sso/3rd_qrConnect?appid="+appid+"&redirect_uri="+redirectUri_encode+"&state="+state+"&usertype=member"; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return qywxAuthUrl; }
必须在指定域名下请求这个url ,然后拿到auth_code
2。获取登录用户信息
先获取服务商凭证
/**
* 获取服务商凭证
*/
public String getProviderToken(String corpid,String providerSecret) {
String providerToken = null;
if (StringUtils.isNotEmpty(corpid) && StringUtils.isNotEmpty(providerSecret)) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_provider_token";
JSONObject parma = new JSONObject();
parma.put("corpid", corpid);
parma.put("provider_secret", providerSecret);
String str = restTemplate.postForObject(url, parma.toString(), String.class);
JSONObject response = JSONObject.parseObject(str);
if (response.containsKey("provider_access_token")) {
providerToken = response.getString("provider_access_token");
}
}
return providerToken;
}
获取登录用户信息
public String getUserInfo(String accessToken,String authCode) { String userInfo = null; if (StringUtils.isNotEmpty(accessToken) && StringUtils.isNotEmpty(authCode)) { String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_login_info?access_token=" + accessToken; JSONObject parma = new JSONObject(); parma.put("auth_code", authCode); String str = restTemplate.postForObject(url, parma.toString(), String.class); JSONObject response = JSONObject.parseObject(str); if (response.containsKey("user_info")) { userInfo = response.getString("user_info"); } } return userInfo; }
先扫码绑定用户信息 再登录校验用户信息