Java 实现钉钉扫码登陆

开发准备: 请先仔细阅读官方文档:扫码登陆
扫码登陆的appId及appSecret(用于扫码登陆), 企业内部的appId及appSecret (用于获取token)
权限申请 官方文档:权限申请
Java 实现钉钉扫码登陆_第1张图片
引入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>
     * 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)