微信企业号OAuth2.0验证接口的调用步骤

第一步:登陆微信后台管理系统。然后找到自己的企业号的应用。p配置可信用域名。

第二步:企业获取code

Https请求方式:GET

https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect

重定向地址:QY_REDIRECT_URL = http://你的可信用域名/工程名/servlet名

根据code获取成员信息

Https请求方式:GET

https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE

这里的access_token获取是用https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s



以下看看我的案例:

将链接信息存放在一个配置文件里config.properties:

#CorpId
CORP_ID = 你的企业号CORP_ID

#CorpSecret
CORP_SECRET = 你的企业号CORP_SECRET

#QyRedirecrUrl
REDIRECT_URL = http://lantingyue11.xicp.io/wechat-web/anon/doBusiness.do

#QyAuthorizeUrl
AUTHORIZE_URL = https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

#QyAccessTokenUrl
QY_ACCESS_TOKEN_URL = https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s



Controller代码:

public abstract class WechatAuthController {


public static final String CORP_ID = PropertyManager.getString(
"config.properties", "CORP_ID");


public static final String CORP_SECRET = PropertyManager.getString(
"config.properties", "CORP_SECRET");


public static final String REDIRECT_URL = PropertyManager.getString(
"config.properties", "REDIRECT_URL");


public static final String AUTHORIZE_URL = PropertyManager.getString(
"config.properties", "AUTHORIZE_URL");


public static final String QY_ACCESS_TOKEN_URL = PropertyManager.getString(
"config.properties", "QY_ACCESS_TOKEN_URL");


public static final String USER_INFO_URL = PropertyManager.getString(
"config.properties", "USER_INFO_URL");


public static String no;


public static String tel;
@RequestMapping(value = "/anon/doBusiness")
public String doBusiness(Model model, @RequestParam String code)
throws IOException {
if (StringUtils.isNullOrEmpty(code)) {
String authRedirectUrl = String.format(AUTHORIZE_URL, CORP_ID,
URLEncoder.encode(REDIRECT_URL, "UTF-8"));
return "redirect:" + authRedirectUrl;
} else {
String accessTokenUrl = String.format(QY_ACCESS_TOKEN_URL, CORP_ID,
CORP_SECRET);
String accessTokenStr = HttpConnectionUtils.doGet(accessTokenUrl);
JSONObject accessTokenObj = JSON.parseObject(accessTokenStr);
String accessToken = accessTokenObj.getString("access_token");
String userInfoUrl = String.format(USER_INFO_URL, accessToken,
code);
String userInfoStr = HttpConnectionUtils.doGet(userInfoUrl);
JSONObject userInfoObj = JSONObject.parseObject(userInfoStr);
String userid = userInfoObj.getString("UserId");
//String deviceid = userInfoObj.getString("DeviceId");


model.addAttribute("userid", userid);
//model.addAttribute("tel", deviceid);
   System.out.println(userid);
no=userid;
//tel=deviceid;
return doBusinessDetail(model, code);

}
}


public abstract String doBusinessDetail(Model model,
@RequestParam String code) throws IOException;


}


你可能感兴趣的:(微信企业号OAuth2.0验证接口的调用步骤)