From db007c3c6c61d7925009c0dc8c1fb7623c96c378 Mon Sep 17 00:00:00 2001 From: blueeon Date: Thu, 28 Mar 2024 23:41:25 +0800 Subject: [PATCH] fix: update token refresh method --- src/lib/SunoApi.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/lib/SunoApi.ts b/src/lib/SunoApi.ts index dd2ef72..adc24fc 100644 --- a/src/lib/SunoApi.ts +++ b/src/lib/SunoApi.ts @@ -31,6 +31,7 @@ class SunoApi { private readonly client: AxiosInstance; private sid?: string; + private currentToken?: string; constructor(cookie: string) { const cookieJar = new CookieJar(); @@ -43,6 +44,12 @@ class SunoApi { 'Cookie': cookie } })) + this.client.interceptors.request.use((config) => { + if (this.currentToken) { // 使用当前token的状态 + config.headers['Authorization'] = `Bearer ${this.currentToken}`; + } + return config; + }); } public async init(): Promise { @@ -78,11 +85,9 @@ class SunoApi { await sleep(1, 2); } const newToken = renewResponse.data['jwt']; + console.log("newToken:===\n\n", newToken); // 更新请求头中的Authorization字段,使用新的JWT令牌 - this.client.interceptors.request.use(function (config) { - config.headers['Authorization'] = `Bearer ${newToken}` - return config; - }) + this.currentToken = newToken; } public async generate(