一个.net开发的web应用接入到企业微信的应用中,实现微信用户点击应用,打开web,获取到用户信息,并实现自动登录功能。
企业微信官方API文档:https://work.weixin.qq.com/api/doc/90000/90135/91020
获取access_token是调用企业微信API接口的第一步,相当于创建了一个登录凭证,其它的业务API接口,都需要依赖于access_token来鉴权调用者身份。
请求方式: GET(HTTPS)
请求地址: https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
注:此处标注大写的单词ID和SECRET,为需要替换的变量,根据实际获取值更新。其它接口也采用相同的标注,不再说明。
权限说明:
每个应用有独立的secret,获取到的access_token只能本应用使用,所以每个应用的access_token应该分开来获取
返回结果:
{
"errcode": 0,
"errmsg": "ok",
"access_token": "accesstoken000001",
"expires_in": 7200
}
如果企业需要在打开的网页里面携带用户的身份信息,第一步需要构造如下的链接来获取code参数:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
员工点击后,页面将跳转至 redirect_uri?code=CODE&state=STATE,企业可根据code参数获得员工的userid。code长度最大为512字节。
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxCorpId&redirect_uri=http%3a%2f%2fwww.wanwone.com%2fcgi-bin%2fquery%3faction%3dget&response_type=code&scope=snsapi_base&state=#wechat_redirect
注意,构造OAuth2链接中参数的redirect_uri是经过UrlEncode的
员工点击后,页面将跳转至
http://www.wanwone.com?action=get&code=AAAAAAgG333qs9EdaPbCAP1VaOrjuNkiAZHTWgaWsZQ&state=
企业可根据code参数调用获取员工的信息.
该接口用于根据code获取成员信息
请求方式:GET(HTTPS)
请求地址:https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
参数说明:
权限说明:
跳转的域名须完全匹配access_token对应应用的可信域名,否则会返回50001错误。
返回结果:
a) 当用户为企业成员时返回示例如下:
{
"errcode": 0,
"errmsg": "ok",
"UserId":"USERID",
"DeviceId":"DEVICEID"
}
b) 非企业成员授权时返回示例如下:
{
"errcode": 0,
"errmsg": "ok",
"OpenId":"OPENID",
"DeviceId":"DEVICEID"
}
protected void Page_Load(object sender, EventArgs e)
{
//获取配置信息(CorpId,Secret)
Dictionary<string, string> configs = GetConfig();
string access_token = Context.Cache["access_token"] as string;
if (!(Context.Request["code"] != null && Context.Request["state"] != null && Context.Request["state"].ToString() == "parasaga"))
{
if (access_token == null)
{
access_token = WXQYHelper.GetAccess_token(configs["CorpId"], configs["Secret"]);
//缓存到Cache
Context.Cache.Insert("access_token", access_token, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(30));
}
if (string.IsNullOrEmpty(access_token))
{
//获取token失败
}
else
{
string service = Context.Request.Url.GetLeftPart(UriPartial.Path);
//构造网页授权链接
string redirectUrl = WXQYHelper.GetAuthLink(configs["CorpId"], service);
//重定向到授权链接
Context.Response.Redirect(redirectUrl);
}
}
else
{
string code = Context.Request["code"].ToString();
//根据code获取访问用户身份
string uid = WXQYHelper.GetUserIdByCode(access_token, code);
if (string.IsNullOrEmpty(uid))
{
//获取失败
}
else
{
//获取成功
//进行应用本身的登录逻辑
}
}
}
public Dictionary<string, string> GetConfig()
{
Dictionary<string, string> configs = new Dictionary<string, string>();
string CorpId = System.Configuration.ConfigurationManager.AppSettings["CorpId"].ToString();
string Secret = System.Configuration.ConfigurationManager.AppSettings["Secret"].ToString();
configs.Add("CorpId", CorpId);
configs.Add("Secret", Secret);
return configs;
}
public class WXQYHelper
{
///
/// 获取登陆标识access_token
///
///
public static string GetAccess_token(string corpId,string secret)
{
string result = "";
try
{
//https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
string tokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
var getStr = "corpid=" + corpId + "&corpsecret=" + secret;
result = Common.HttpWebRequest(tokenUrl, getStr, "GET");
}
catch (Exception e)
{
//PsLog.Error("获取登陆标识失败,返回结果:" + e.ToString());
return "";
}
//{
// "errcode": 0,
// "errmsg": "ok",
// "access_token": "accesstoken000001",
// "expires_in": 7200
//}
var jsonObj = (JObject)JsonConvert.DeserializeObject(result);
var token = jsonObj["access_token"].ToString();
if (!string.IsNullOrEmpty(token))
{
return token;
}
else
{
return "";
}
}
///
/// 构造网页授权链接
///
///
///
///
public static string GetAuthLink(string corpId,string appUrl)
{
string link = "";
//https://open.weixin.qq.com/connect/oauth2/authorize
//?appid=wxCorpId
//&redirect_uri=http%3a%2f%2fapi.3dept.com%2fcgi-bin%2fquery%3faction%3dget
//&response_type=code
//&scope=snsapi_base
//&state=#wechat_redirect
link = string.Format("{0}?appid={1}&redirect_uri={2}&response_type={3}&scope={4}&state={5}#wechat_redirect",
"https://open.weixin.qq.com/connect/oauth2/authorize",
corpId,
HttpUtility.UrlEncode(appUrl),
"code",
"snsapi_base",
"parasaga"
);
return link;
}
///
/// 获取访问用户身份
///
///
///
///
public static string GetUserIdByCode(string accessToken, string code)
{
//https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
string result = "";
try
{
//https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
string tokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo";
var getStr = "access_token=" + accessToken + "&code=" + code;
result = Common.HttpWebRequest(tokenUrl, getStr, "GET");
}
catch (Exception e)
{
//PsLog.Error("获取userid失败,返回结果:" + e.ToString());
return "";
}
//{
// "errcode": 0,
// "errmsg": "ok",
// "UserId":"USERID",
// "DeviceId":"DEVICEID"
//}
var jsonObj = (JObject)JsonConvert.DeserializeObject(result);
var uid = jsonObj["UserId"].ToString();
if (!string.IsNullOrEmpty(uid))
{
return uid;
}
else
{
return "";
}
}
}