mirror of https://github.com/ZhongFuCheng3y/austin
commit
6f7ed66121
@ -0,0 +1,37 @@
|
||||
package com.java3y.austin.support.dto;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author 3y
|
||||
* @date 2022/5/8
|
||||
* <p>
|
||||
* https://docs.getui.com/getui/server/rest_v2/token/
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class GeTuiTokenResultDTO {
|
||||
|
||||
|
||||
@JSONField(name = "msg")
|
||||
private String msg;
|
||||
@JSONField(name = "code")
|
||||
private Integer code;
|
||||
@JSONField(name = "data")
|
||||
private DataDTO data;
|
||||
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
public static class DataDTO {
|
||||
@JSONField(name = "expire_time")
|
||||
private String expireTime;
|
||||
@JSONField(name = "token")
|
||||
private String token;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.java3y.austin.support.dto;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
|
||||
/**
|
||||
* 请求token时的参数
|
||||
*
|
||||
* @author 3y
|
||||
* https://docs.getui.com/getui/server/rest_v2/token/
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
public class QueryTokenParamDTO {
|
||||
/**
|
||||
* sign
|
||||
*/
|
||||
@JSONField(name = "sign")
|
||||
private String sign;
|
||||
/**
|
||||
* timestamp
|
||||
*/
|
||||
@JSONField(name = "timestamp")
|
||||
private String timestamp;
|
||||
/**
|
||||
* appkey
|
||||
*/
|
||||
@JSONField(name = "appkey")
|
||||
private String appKey;
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package com.java3y.austin.support.utils;
|
||||
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.http.ContentType;
|
||||
import cn.hutool.http.Header;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.dingtalk.api.DefaultDingTalkClient;
|
||||
import com.dingtalk.api.DingTalkClient;
|
||||
import com.dingtalk.api.request.OapiGettokenRequest;
|
||||
import com.dingtalk.api.response.OapiGettokenResponse;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.java3y.austin.common.constant.CommonConstant;
|
||||
import com.java3y.austin.common.dto.account.DingDingWorkNoticeAccount;
|
||||
import com.java3y.austin.common.dto.account.GeTuiAccount;
|
||||
import com.java3y.austin.support.dto.GeTuiTokenResultDTO;
|
||||
import com.java3y.austin.support.dto.QueryTokenParamDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 获取第三发token工具类
|
||||
*/
|
||||
@Slf4j
|
||||
public class AccessTokenUtils {
|
||||
|
||||
/**
|
||||
* 钉钉获取token地址
|
||||
*/
|
||||
private static final String DING_DING_TOKEN_URL = "https://oapi.dingtalk.com/gettoken";
|
||||
|
||||
/**
|
||||
* 获取钉钉 access_token
|
||||
*
|
||||
* @param account 钉钉工作消息 账号信息
|
||||
* @return 钉钉 access_token
|
||||
*/
|
||||
public static String getDingDingAccessToken(DingDingWorkNoticeAccount account) {
|
||||
String accessToken = "";
|
||||
try {
|
||||
DingTalkClient client = new DefaultDingTalkClient(DING_DING_TOKEN_URL);
|
||||
OapiGettokenRequest req = new OapiGettokenRequest();
|
||||
req.setAppkey(account.getAppKey());
|
||||
req.setAppsecret(account.getAppSecret());
|
||||
req.setHttpMethod(CommonConstant.REQUEST_METHOD_GET);
|
||||
OapiGettokenResponse rsp = client.execute(req);
|
||||
accessToken = rsp.getAccessToken();
|
||||
} catch (Exception e) {
|
||||
log.error("AccessTokenUtils#getDingDingAccessToken fail:{}", Throwables.getStackTraceAsString(e));
|
||||
}
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取个推的 access_token
|
||||
*
|
||||
* @param account 创建个推账号时的元信息
|
||||
* @return 个推的 access_token
|
||||
*/
|
||||
public static String getGeTuiAccessToken(GeTuiAccount account) {
|
||||
String accessToken = "";
|
||||
try {
|
||||
String url = "https://restapi.getui.com/v2/" + account.getAppId() + "/auth";
|
||||
String time = String.valueOf(System.currentTimeMillis());
|
||||
String digest = SecureUtil.sha256().digestHex(account.getAppKey() + time + account.getMasterSecret());
|
||||
QueryTokenParamDTO param = QueryTokenParamDTO.builder()
|
||||
.timestamp(time)
|
||||
.appKey(account.getAppKey())
|
||||
.sign(digest).build();
|
||||
|
||||
String body = HttpRequest.post(url).header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue())
|
||||
.body(JSON.toJSONString(param))
|
||||
.timeout(20000)
|
||||
.execute().body();
|
||||
GeTuiTokenResultDTO geTuiTokenResultDTO = JSON.parseObject(body, GeTuiTokenResultDTO.class);
|
||||
if (geTuiTokenResultDTO.getCode().equals(0)) {
|
||||
accessToken = geTuiTokenResultDTO.getData().getToken();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("AccessTokenUtils#getGeTuiAccessToken fail:{}", Throwables.getStackTraceAsString(e));
|
||||
}
|
||||
return accessToken;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue