以下信息根据这个文档来进行讲解,其中进行了补充,使得文档更加易于理解(因为这方面netcore参考不多,我收集现在资料出一个较为详细的操作信息)
实现登录第三方网站文档
这里需要账号权限,需要公司给与开通相应得权限,这里默认为有权限。
钉钉开发者后台
现在开发后台打开得都是默认新版页面,但是我没找到创建第三方登录授权应用按钮,所以接下来都是以旧版界面为例
这里开始往下都是旧版模式界面
这里特别注意
第三和这第四步创建的应用用途不一样
弟三步创建的应用,是用来登录的(我现在觉得理解为登录接口api合适点,应用有点抽象),这里的appkey和secrect是用来调取二维码用的
这第四步是用来创建真正的应用,你项目(第三方网站)所依赖的应用
因为钉钉需要请求后端服务的接口,但在开发前期你需要测试,或者你处于公司内网,钉钉调取不到你的接口,所以需要进行内网穿透,来让钉钉服务能通过外网访问到你的主机。
点击这里查看我收集的两种内网穿透方法文章
为了方便这里没有用前后分离,直接使用netcoremvc开发
在这里是使用的这位博主得代码,是在他得基础上进一步详细讲解和补充(controller修改了部分代码,适应新版sdk,view也就是前端页面只修改了几个配置,下面会指出,其他都是没有改动的)
代码下载
SDK文档地址
新版sdk
新版sdk直接解压
旧版sdk(netcore版本,net版本还未尝试,自行摸索)
解压红框内的文件
右键项目解决方案-》添加-》项目引用,让后分别找到上面下载sdk的csproj添加上此时项目结构如上。
右键DingQrCodeLogin-》添加-》项目应用然后添加上这两个sdk的引用
==注意本文涉及了两个应用,一个是登录授权应用,一个是你自己的三方应用,这俩分别都有一对appid(key)和appsecret。不要设置混淆了,这是是设置为自己三方应用的。
这里用户扫扫码后会给homecontroller传递code和state两个参数,state是固定的不用管
修改刚才下载下来的homecontroller代码
什么都没干,先定义方法接收,扫码后传递过来的参数
public string DingLogin(string code, string state)
{
}
api调试台
免密登录,需要access_token才行
public static AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client CreateClient()
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config();
config.Protocol = "https";
config.RegionId = "central";
return new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client(config);
}
public string DingLogin(string code, string state)
{
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client client = CreateClient();
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest getAccessTokenRequest = new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest();
try
{
// 这里获得了咱们的access_token
client.GetAccessToken { AppKey = "自己的外部appkey",AppSecret = "自己。。。" };
}
}
public static AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client CreateClient()
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config();
config.Protocol = "https";
config.RegionId = "central";
return new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client(config);
}
public string DingLogin(string code, string state)
{
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client client = CreateClient();
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest getAccessTokenRequest = new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest();
try
{
// 这里获得了咱们的access_token
client.GetAccessToken { AppKey = "自己的外部appkey",AppSecret = "自己。。。" };
//通过扫码获得的code和上面获取的access_token,获取unionid
OapiSnsGetuserinfoBycodeResponse response = new OapiSnsGetuserinfoBycodeResponse();
string qrAppId = AppConfigurtaionHelper.Configuration["DingDing:QrAppId"];
string qrAppSecret = AppConfigurtaionHelper.Configuration["DingDing:QrAppSecret"];
if (string.IsNullOrWhiteSpace(qrAppId) || string.IsNullOrWhiteSpace(qrAppSecret))
{
throw new Exception("钉钉配置信息错误!");
}
DefaultDingTalkClient uclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
OapiSnsGetuserinfoBycodeRequest ureq = new OapiSnsGetuserinfoBycodeRequest();
ureq.TmpAuthCode = code;
response = uclient.Execute(ureq, qrAppId, qrAppSecret);
}
}
public static AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client CreateClient()
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config();
config.Protocol = "https";
config.RegionId = "central";
return new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client(config);
}
public string DingLogin(string code, string state)
{
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client client = CreateClient();
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest getAccessTokenRequest = new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest();
try
{
// 这里获得了咱们的access_token
client.GetAccessToken { AppKey = "自己的外部appkey",AppSecret = "自己。。。" };
//通过扫码获得的code和上面获取的access_token,获取unionid
OapiSnsGetuserinfoBycodeResponse response = new OapiSnsGetuserinfoBycodeResponse();
string qrAppId = AppConfigurtaionHelper.Configuration["DingDing:QrAppId"];
string qrAppSecret = AppConfigurtaionHelper.Configuration["DingDing:QrAppSecret"];
if (string.IsNullOrWhiteSpace(qrAppId) || string.IsNullOrWhiteSpace(qrAppSecret))
{
throw new Exception("钉钉配置信息错误!");
}
DefaultDingTalkClient uclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
OapiSnsGetuserinfoBycodeRequest ureq = new OapiSnsGetuserinfoBycodeRequest();
ureq.TmpAuthCode = code;
response = uclient.Execute(ureq, qrAppId, qrAppSecret);
//获取用户id
IDingTalkClient ubclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid");
OapiUserGetbyunionidRequest ubreq = new OapiUserGetbyunionidRequest { Unionid = response.UserInfo.Unionid};
OapiUserGetbyunionidResponse ubrsp = ubclient.Execute(ubreq, a.Body.AccessToken);
Console.WriteLine(ubrsp.Body);
}
}
public static AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client CreateClient()
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config();
config.Protocol = "https";
config.RegionId = "central";
return new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client(config);
}
public string DingLogin(string code, string state)
{
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client client = CreateClient();
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest getAccessTokenRequest = new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest();
try
{
// 这里获得了咱们的access_token
client.GetAccessToken { AppKey = "自己的外部appkey",AppSecret = "自己。。。" };
//通过扫码获得的code和上面获取的access_token,获取unionid
OapiSnsGetuserinfoBycodeResponse response = new OapiSnsGetuserinfoBycodeResponse();
string qrAppId = AppConfigurtaionHelper.Configuration["DingDing:QrAppId"];
string qrAppSecret = AppConfigurtaionHelper.Configuration["DingDing:QrAppSecret"];
if (string.IsNullOrWhiteSpace(qrAppId) || string.IsNullOrWhiteSpace(qrAppSecret))
{
throw new Exception("钉钉配置信息错误!");
}
DefaultDingTalkClient uclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
OapiSnsGetuserinfoBycodeRequest ureq = new OapiSnsGetuserinfoBycodeRequest();
ureq.TmpAuthCode = code;
response = uclient.Execute(ureq, qrAppId, qrAppSecret);
//获取用户id
IDingTalkClient ubclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid");
OapiUserGetbyunionidRequest ubreq = new OapiUserGetbyunionidRequest { Unionid = response.UserInfo.Unionid};
OapiUserGetbyunionidResponse ubrsp = ubclient.Execute(ubreq, a.Body.AccessToken);
Console.WriteLine(ubrsp.Body);
//获取用户信息
IDingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
OapiV2UserGetRequest req2 = new OapiV2UserGetRequest { Userid = ubrsp.Result.Userid };
OapiV2UserGetResponse rsp2 = client2.Execute(req2, a.Body.AccessToken);
}
}
剩下的就需要直接自己去根据需求进行探索
功能api
功能api