* 启动 OAuth 设备授权流程 * @param {Object} [account] - Qwen 账户对象(用于解析账号级代理) * @returns {Promise } 包含设备代码、验证URL和代码验证器的对象
(account)
| 65 | * @returns {Promise<Object>} 包含设备代码、验证URL和代码验证器的对象 |
| 66 | */ |
| 67 | async initiateDeviceFlow(account) { |
| 68 | // 生成 PKCE 代码验证器和挑战 |
| 69 | const { code_verifier, code_challenge } = generatePKCEPair() |
| 70 | |
| 71 | const bodyData = new URLSearchParams({ |
| 72 | client_id: "f0304373b74a44d2b584a3fb70ca9e56", |
| 73 | scope: "openid profile email model.completion", |
| 74 | code_challenge: code_challenge, |
| 75 | code_challenge_method: 'S256', |
| 76 | }) |
| 77 | |
| 78 | const chatBaseUrl = getChatBaseUrl() |
| 79 | |
| 80 | const fetchOptions = { |
| 81 | method: 'POST', |
| 82 | headers: { |
| 83 | 'Content-Type': 'application/x-www-form-urlencoded', |
| 84 | Accept: 'application/json', |
| 85 | }, |
| 86 | body: bodyData, |
| 87 | } |
| 88 | |
| 89 | applyProxyToFetchOptions(fetchOptions, account) |
| 90 | |
| 91 | try { |
| 92 | const response = await fetch(`${chatBaseUrl}/api/v1/oauth2/device/code`, fetchOptions) |
| 93 | |
| 94 | if (response.ok) { |
| 95 | const result = await response.json() |
| 96 | return { |
| 97 | status: true, |
| 98 | ...result, |
| 99 | code_verifier: code_verifier |
| 100 | } |
| 101 | } else { |
| 102 | const responseBody = await this.readResponseBody(response) |
| 103 | logger.error('CLI设备授权初始化失败', 'CLI', '', { |
| 104 | status: response.status, |
| 105 | statusText: response.statusText, |
| 106 | body: responseBody |
| 107 | }) |
| 108 | throw new Error('device_flow_failed') |
| 109 | } |
| 110 | } catch (error) { |
| 111 | logger.error('CLI设备授权流程异常', 'CLI', '', { |
| 112 | url: `${chatBaseUrl}/api/v1/oauth2/device/code`, |
| 113 | message: error.message |
| 114 | }) |
| 115 | return { |
| 116 | status: false, |
| 117 | device_code: null, |
| 118 | user_code: null, |
| 119 | verification_uri: null, |
| 120 | verification_uri_complete: null, |
| 121 | expires_in: null, |
| 122 | code_verifier: null |
| 123 | } |
| 124 | } |
no test coverage detected