企业微信应用接入获取UserId流程
- 1 创建应用,获取企业ID + 应用ID(AgentId) + 应用密钥(Secret)
- 2 通过
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
接口获取到accessToken,并存入redis or 全局静态变量中 - 3 配置可信域名 (可以域名+端口)
- 4-1 JAVA验证方式一
@RequestMapping("/")
public class TxtApi {
@GetMapping("你得文件名称.txt")
@ResponseBody
public String text(){
return "你得文件名称内容";
}
}
server {
listen 你的端口;
server_name localhost;
location / {
root C:\Users\Administrator\Desktop\;
}
}
@Controller
@RequestMapping("api/sys")
@Slf4j
public class SystemController {
@GetMapping("getCode")
public ModelAndView getCode(String code, HttpSession session){
String result = HttpUtil.getResult("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE".replace("ACCESS_TOKEN", accessToken(以前的文章有说怎么获取)).replace("CODE", code));
JSONObject jsonObject = JSONObject.fromObject(result);
log.info("获取数据 {}",jsonObject);
String userId = jsonObject.getString("UserId");
return new ModelAndView(new RedirectView("前端页面路径?userId="+userId));
}
@GetMapping("buildCode")
public String buildCode(){
String buildUrl = "";
String url = "可信域名前缀" + "api/sys/getCode";
return "redirect:"+ "https://open.weixin.qq.com/connect/oauth2/authorize"
+ "?appid=CORPID"
+ "&redirect_uri=REDIRECT_URI"
+ "&response_type=code"
+ "&scope=snsapi_base"
+ "&state=STATE"
+ "#wechat_redirect".replace("CORPID", CORP_ID).replace("REDIRECT_URI", url);
}
}
- 6.成功再页面上获取用户数据