企業微信應用授權登入
阿新 • • 發佈:2020-12-26
前言
企微建立一個新應用,使用者點選靜默授權登入
企業微信API
操作
這邊不按部就班,使用一次性訪問
建立一個連結
標準連結
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
賦值
https://open.weixin.qq.com/connect/oauth2/authorize?appid=A& redirect_uri=B&response_type=code&scope=snsapi_base&agentid=C&state=D
- A appid 填寫對應引數
- B 重定向連結 獲取code 需要URLEncoder轉碼
- C agentid 填寫對應引數
- D 微信重定向後攜帶的引數(建議寫前端地址或者其他)、
注意:拼接完成的連結需要配置在企微對應主頁地址
重定向
/**
* 微信使用者登入獲取使用者
*
* @param code 回撥
* @param state
* @param request
* @return
* @throws IOException
*/
@GetMapping("oauth2me.do")
@ApiOperation(value = "回撥方法")
public RedirectView getuserinfo(@RequestParam String code, @RequestParam String state, HttpServletRequest request) throws IOException {
System.out.println("回撥方法code: " + code + "state: " + state);
BaseResult getuserinfo = weChatServicel.getuserinfo(code, request);
String redirectUrl = "";
if (getuserinfo.getErrno() == 200) {
Object o = JSONArray.toJSON(data);
String s = Base64Util.encryptBASE64(o.toString());
String encode = URLEncoder.encode(s, "UTF-8");
redirectUrl = state + "?" + encode;
} else {
redirectUrl = state;
}
System.out.println("重定向URL: " + redirectUrl);
return new RedirectView(redirectUrl);
}
獲取訪問使用者身份
方法體內部呼叫這個請求就行
請求方式:GET(HTTPS)
請求地址:https://qyapi.weixin.qq.com/cgi-bin/service/getuserinfo3rd?suite_access_token=SUITE_ACCESS_TOKEN&code=CODE
返回值
{
"errcode": 0,
"errmsg": "ok",
"CorpId":"CORPID",
"UserId":"USERID",
"DeviceId":"DEVICEID",
"user_ticket": "USER_TICKET",
"expires_in":7200,
"open_userid":"wwxxxx"
}