Skip to content

Commit

Permalink
fix(lark): fix refresh interval for access token
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 10, 2025
1 parent 1858596 commit 82dbae3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion adapters/lark/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/adapter-lark",
"description": "Lark (飞书) Adapter for Satorijs",
"version": "3.9.1",
"version": "3.9.4",
"type": "module",
"main": "lib/index.cjs",
"types": "lib/index.d.ts",
Expand Down
26 changes: 17 additions & 9 deletions adapters/lark/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,26 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
}

private async refreshToken() {
const { tenant_access_token: token } = await this.internal.tenantAccessTokenInternalAuth({
app_id: this.config.appId,
app_secret: this.config.appSecret,
})
this.logger.debug('refreshed token %s', token)
this.token = token
// https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token_internal
// tenant_access_token 的最大有效期是 2 小时。
// 剩余有效期小于 30 分钟时,调用本接口会返回一个新的 tenant_access_token,这会同时存在两个有效的 tenant_access_token。
// 剩余有效期小于 30 分钟时,调用本接口会返回一个新的 tenant_access_token,此时会同时存在两个有效的 tenant_access_token。
// 剩余有效期大于等于 30 分钟时,调用本接口会返回原有的 tenant_access_token。
// https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token_internal
// 初次获得 token 后的半小时内必须刷新一次,因为初次获得的 token 可能是 1.5 小时前生成的。
let timeout = Time.minute * 20
try {
const { tenant_access_token: token } = await this.internal.tenantAccessTokenInternalAuth({
app_id: this.config.appId,
app_secret: this.config.appSecret,
})
this.logger.debug('refreshed token %s', token)
this.token = token
} catch (error) {
this.logger.error('failed to refresh token, retrying in 10s')
this.logger.error(error)
timeout = Time.second * 10
}
if (this._refresher) clearTimeout(this._refresher)
this._refresher = setTimeout(() => this.refreshToken(), Time.minute * 100)
this._refresher = setTimeout(() => this.refreshToken(), timeout)
this.online()
}

Expand Down

0 comments on commit 82dbae3

Please sign in to comment.