抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

构造扫码登录页面

Web系统可以通过两种方式实现钉钉扫码登录

1、使用钉钉提供的扫码登录页面

在企业Web系统里,用户点击使用钉钉扫描登录时第三方Web系统跳转到如下地址:

1
2
//url里的参数需要换成第三方Web系统对应的参数。在钉钉用户扫码登录并确认后,会302到你指定的redirect_uri,并向url参数中追加临时授权码code及state两个参数。
https://oapi.dingtalk.com/connect/qrconnect?appid=SuiteKey&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=REDIRECT_URI

注意:

  • 参数redirect_uri=REDIRECT_URI涉及的域名,需和登录配置的回调域名一致,否则会提示无权限访问。
  • 如果是企业内部应用,appid则为应用的AppKey;如果是第三方企业应用,appid则为应用的SuiteKey。
2、服务端通过临时授权码获取授权用户的个人信息

调用sns/getuserinfo_bycode接口获取授权用户的个人信息,详情请参考
https://open.dingtalk.com/document/orgapp-server/obtain-the-user-information-based-on-the-sns-temporary-authorization

4、根据unionid获取userid。

调用user/getbyunionid接口获取userid,详情请参考
https://open.dingtalk.com/document/orgapp-server/query-a-user-by-the-union-id

根据userid获取用户详情。

调用user/get接口获取用户信息,详情请参考获取用户详情。
https://open.dingtalk.com/document/orgapp-server/query-user-details

4、示例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 扫码登录第三方网站
* @author openapi@dingtalk
*
*/
@RestController
public class ddscan {

/**
* 获取授权用户的个人信息
* openapi@dingtalk
* @return
* @throws Exception
* ServiceResult<Map<String,Object>>
* 2020-11-4
*/
@RequestMapping(value = "/getUserInfo",method = RequestMethod.GET)
public ServiceResult<Map<String, Object>> getUserInfo(@RequestParam("code")String code) throws ApiException {
// 获取access_token,注意正式代码要有异常流处理
String access_token= AccessTokenUtil.getToken();

// 通过临时授权码获取授权用户的个人信息
DefaultDingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
OapiSnsGetuserinfoBycodeRequest reqBycodeRequest = new OapiSnsGetuserinfoBycodeRequest();
// 通过扫描二维码,跳转指定的redirect_uri后,向url中追加的code临时授权码
reqBycodeRequest.setTmpAuthCode(code);
OapiSnsGetuserinfoBycodeResponse bycodeResponse = client2.execute(reqBycodeRequest, "yourAppKey","yourAppSecret");

// 根据unionid获取userid
String unionid = bycodeResponse.getUserInfo().getUnionid();
DingTalkClient clientDingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid");
OapiUserGetbyunionidRequest reqGetbyunionidRequest = new OapiUserGetbyunionidRequest();
reqGetbyunionidRequest.setUnionid(unionid);
OapiUserGetbyunionidResponse oapiUserGetbyunionidResponse = clientDingTalkClient.execute(reqGetbyunionidRequest,access_token);

// 根据userId获取用户信息
String userid = oapiUserGetbyunionidResponse.getResult().getUserid();
DingTalkClient clientDingTalkClient2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
OapiV2UserGetRequest reqGetRequest = new OapiV2UserGetRequest();
reqGetRequest.setUserid(userid);
reqGetRequest.setLanguage("zh_CN");
OapiV2UserGetResponse rspGetResponse = clientDingTalkClient2.execute(reqGetRequest, access_token);
System.out.println(rspGetResponse.getBody());
Map<String, Object> map = new HashMap<String,Object>();
map.put("userInfo", rspGetResponse.getBody());
return ServiceResult.success(map);
}

}

评论