Java 实现钉钉扫码登陆

开发准备: 请先仔细阅读官方文档:扫码登陆
扫码登陆的appId及appSecret(用于扫码登陆), 企业内部的appId及appSecret (用于获取token)
权限申请 官方文档:权限申请
在这里插入图片描述
引入sdk包:
需要使用钉钉的SDK包,用来给钉钉的服务器发送请求,但是钉钉的jar包没有maven的地址,所以需要手动的进行引入。于是将jar和source放置在了src/main/libs下:

pom.xml

<dependency>
            <groupId>taobao-sdk-java-auto_1479188381469-20210207</groupId>
            <artifactId>taobao-sdk-java-auto</artifactId>
            <version>3.2.3</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/libs/taobao-sdk-java-auto_1479188381469-20210207.jar</systemPath>
</dependency>

注意引入jar包就可以了,不用引入source包。

还需要

<plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
		<configuration>
			<includeSystemScope>true</includeSystemScope>
		</configuration>
</plugin>

这样才可以保证打包的时候可以带上钉钉的工具包。

代码如下:

@RestController
@RequestMapping("xx/userLogin")
@Slf4j
public class LoginController {

    /**
     * 获取授权用户的个人信息
     * openapi@dingtalk
     * @return
     * @throws Exception
     * ServiceResult<Map<String,Object>>
     * 2020-11-4
     */
    @RequestMapping(value = "/getUserInfo",method = RequestMethod.POST)
    public void getUserInfo(@RequestParam("authCode")String code,HttpServletRequest request) throws ApiException {
        // 获取access_token,注意正式代码要有异常流处理
        String access_token = this.getAccessToken();
        // 通过临时授权码获取授权用户的个人信息
        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, SysConfigCache.appId, SysConfigCache.appSecret);

        // 根据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());
    }

    /**
     * 获取accessToken
     * @return
     * @throws ApiException
     */
    public String getAccessToken() throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(SysConfigCache.innerAppId);
        request.setAppsecret(SysConfigCache.innerAppSecret);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = client.execute(request);
        return response.getAccessToken();
    }

}

如遇到以下错误信息,附解决方法
错误:访问ip不在白名单之中,request ip=58.246.10.114
解决方案:在应用的ip出口白名单添加公网ip 访问ip不在白名单之中?

在linux系统中输入以下命令,可以查看到服务器连接的公网信息
curl cip.cc

钉钉 Java SDK 钉钉官方提供了统一的SDK,使用SDK可以便捷的调用服务端API。注意:此SDK包含原有TOP接口,原有TOP接口可以继续正常调用,没有影响。下面是使用SDK调用API的请求示例: JAVA: DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get"); OapiUserGetRequest req = new OapiUserGetRequest(); req.setUserid("userid1"); req.setHttpMethod("GET"); OapiUserGetResponse rsp = client.execute(req, accessToken); 请求示例说明: (1)定义Client对象,设置接口地址URI。一般地,无需拼接URL后面的参数,比如access_token;需要注意部分Post接口,需要在URI后拼接除access_token外的个别参数。 (2)构造Request对象,接口对应的request对象,一般规则是Oapi+接口Path驼峰形式+Request,如/user/get接口,请求对象是OapiUserGetRequest。 (3)调用Request的方法设置参数,注意参数HttpMethod,默认是POST,如果是GET类型接口,需要这样设置request.setHttpMethod("GET")。 (4)调用client.execute执行,其中access_token同样可以使用SDK获取到。同时,/gettoken,/sns/gettoken, /service/get_suite_token等获取token的接口在调用execute时,无需再传token参数。 (5)执行后得到Response对象,是与Request对应的,然后可获取执行结果。 环境依赖 (1)JAVA SDK 需要依赖 Java SE/EE 1.5及以上。 (2).NET SDK 需要依赖 .NET Framework 2.0及以上 (不支持Windows Phone平台)。 (3)Python SDK需要依赖python2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值