* 刷新访问令牌 * @param {Object} CliAccount - 账户信息 * @param {Object} [account] - Qwen 账户对象(用于解析账号级代理) * @returns {Promise } 账户信息
(CliAccount, account)
| 294 | * @returns {Promise<Object>} 账户信息 |
| 295 | */ |
| 296 | async refreshAccessToken(CliAccount, account) { |
| 297 | try { |
| 298 | |
| 299 | if (!CliAccount || !CliAccount.refresh_token) { |
| 300 | throw new Error() |
| 301 | } |
| 302 | |
| 303 | const chatBaseUrl = getChatBaseUrl() |
| 304 | |
| 305 | const bodyData = new URLSearchParams({ |
| 306 | grant_type: 'refresh_token', |
| 307 | refresh_token: CliAccount.refresh_token, |
| 308 | client_id: "f0304373b74a44d2b584a3fb70ca9e56", |
| 309 | }) |
| 310 | |
| 311 | const fetchOptions = { |
| 312 | method: 'POST', |
| 313 | headers: { |
| 314 | 'Content-Type': 'application/x-www-form-urlencoded', |
| 315 | Accept: 'application/json', |
| 316 | }, |
| 317 | body: bodyData |
| 318 | } |
| 319 | |
| 320 | applyProxyToFetchOptions(fetchOptions, account) |
| 321 | |
| 322 | const response = await fetch(`${chatBaseUrl}/api/v1/oauth2/token`, fetchOptions) |
| 323 | |
| 324 | if (response.ok) { |
| 325 | const tokenData = await response.json() |
| 326 | |
| 327 | return { |
| 328 | access_token: tokenData.access_token, |
| 329 | refresh_token: tokenData.refresh_token || CliAccount.refresh_token, |
| 330 | expiry_date: Date.now() + tokenData.expires_in * 1000, |
| 331 | } |
| 332 | } |
| 333 | } catch (error) { |
| 334 | return { |
| 335 | status: false, |
| 336 | access_token: null, |
| 337 | refresh_token: null, |
| 338 | expiry_date: null |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | } |
| 344 |
no test coverage detected