fix: lark calendar refresh token bug (#4066)
Co-authored-by: chengcheng.frontend <chengcheng.frontend@bytedance.com> Co-authored-by: Joe Au-Yeung <j.auyeung419@gmail.com> Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>pull/4047/head
parent
397d47b4b5
commit
6c3dcc2fbf
|
@ -107,7 +107,7 @@ export const getAppAccessToken: () => Promise<string> = async () => {
|
||||||
const expireDate = appKeys.expire_date;
|
const expireDate = appKeys.expire_date;
|
||||||
|
|
||||||
if (appAccessToken && expireDate && !isExpired(expireDate)) {
|
if (appAccessToken && expireDate && !isExpired(expireDate)) {
|
||||||
log.debug("get app access token not expired", appAccessToken);
|
log.debug("get app access token not expired");
|
||||||
return appAccessToken;
|
return appAccessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +137,7 @@ export const getAppAccessToken: () => Promise<string> = async () => {
|
||||||
where: { slug: "lark-calendar" },
|
where: { slug: "lark-calendar" },
|
||||||
data: { keys: { ...appKeys, app_ticket: "" } },
|
data: { keys: { ...appKeys, app_ticket: "" } },
|
||||||
});
|
});
|
||||||
return getAppAccessToken();
|
throw new Error("app_ticket invalid, please try again");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,7 @@ export default class LarkCalendarService implements Calendar {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const appAccessToken = await getAppAccessToken();
|
const appAccessToken = await getAppAccessToken();
|
||||||
const resp = await this.fetcher(`/authen/v1/refresh_access_token`, {
|
const resp = await fetch(`${this.url}/authen/v1/refresh_access_token`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${appAccessToken}`,
|
Authorization: `Bearer ${appAccessToken}`,
|
||||||
|
@ -84,8 +84,15 @@ export default class LarkCalendarService implements Calendar {
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await handleLarkError<RefreshTokenResp>(resp, this.log);
|
const data = await handleLarkError<RefreshTokenResp>(resp, this.log);
|
||||||
|
this.log.debug(
|
||||||
|
"LarkCalendarService refreshAccessToken data refresh_expires_in",
|
||||||
|
data.data.refresh_expires_in,
|
||||||
|
"and access token expire in",
|
||||||
|
data.data.expires_in
|
||||||
|
);
|
||||||
const newLarkAuthCredentials: LarkAuthCredentials = {
|
const newLarkAuthCredentials: LarkAuthCredentials = {
|
||||||
...larkAuthCredentials,
|
refresh_token: data.data.refresh_token,
|
||||||
|
refresh_expires_date: Math.round(+new Date() / 1000 + data.data.refresh_expires_in),
|
||||||
access_token: data.data.access_token,
|
access_token: data.data.access_token,
|
||||||
expiry_date: Math.round(+new Date() / 1000 + data.data.expires_in),
|
expiry_date: Math.round(+new Date() / 1000 + data.data.expires_in),
|
||||||
};
|
};
|
||||||
|
@ -107,7 +114,13 @@ export default class LarkCalendarService implements Calendar {
|
||||||
};
|
};
|
||||||
|
|
||||||
private fetcher = async (endpoint: string, init?: RequestInit | undefined) => {
|
private fetcher = async (endpoint: string, init?: RequestInit | undefined) => {
|
||||||
const accessToken = await this.auth.getToken();
|
let accessToken = "";
|
||||||
|
try {
|
||||||
|
accessToken = await this.auth.getToken();
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error("get access token error");
|
||||||
|
}
|
||||||
|
|
||||||
return fetch(`${this.url}${endpoint}`, {
|
return fetch(`${this.url}${endpoint}`, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
|
|
Loading…
Reference in New Issue