mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
仅对个人账户调用 tokeninfo/userinfo 接口
- 添加 projectId 非空判断,减少对企业账户的影响 - 优化错误日志级别为 warn
This commit is contained in:
@@ -381,13 +381,18 @@ function handleSimpleEndpoint(apiMethod) {
|
||||
let proxyConfig = null
|
||||
if (account.proxy) {
|
||||
try {
|
||||
proxyConfig = typeof account.proxy === 'string' ? JSON.parse(account.proxy) : account.proxy
|
||||
proxyConfig =
|
||||
typeof account.proxy === 'string' ? JSON.parse(account.proxy) : account.proxy
|
||||
} catch (e) {
|
||||
logger.warn('Failed to parse proxy configuration:', e)
|
||||
}
|
||||
}
|
||||
|
||||
const client = await geminiAccountService.getOauthClient(accessToken, refreshToken, proxyConfig)
|
||||
const client = await geminiAccountService.getOauthClient(
|
||||
accessToken,
|
||||
refreshToken,
|
||||
proxyConfig
|
||||
)
|
||||
|
||||
// 直接转发请求体,不做特殊处理
|
||||
const response = await geminiAccountService.forwardToCodeAssist(
|
||||
@@ -1080,7 +1085,11 @@ router.post('/v1internal\\:onboardUser', authenticateApiKey, handleOnboardUser)
|
||||
router.post('/v1internal\\:countTokens', authenticateApiKey, handleCountTokens)
|
||||
router.post('/v1internal\\:generateContent', authenticateApiKey, handleGenerateContent)
|
||||
router.post('/v1internal\\:streamGenerateContent', authenticateApiKey, handleStreamGenerateContent)
|
||||
router.post('/v1internal\\:listExperiments', authenticateApiKey, handleSimpleEndpoint('listExperiments'))
|
||||
router.post(
|
||||
'/v1internal\\:listExperiments',
|
||||
authenticateApiKey,
|
||||
handleSimpleEndpoint('listExperiments')
|
||||
)
|
||||
|
||||
// v1beta 版本的端点 - 支持动态模型名称
|
||||
router.post('/v1beta/models/:modelName\\:loadCodeAssist', authenticateApiKey, handleLoadCodeAssist)
|
||||
|
||||
@@ -1088,9 +1088,7 @@ async function forwardToCodeAssist(client, apiMethod, requestBody, proxyConfig =
|
||||
axiosConfig.httpAgent = proxyAgent
|
||||
axiosConfig.httpsAgent = proxyAgent
|
||||
axiosConfig.proxy = false
|
||||
logger.info(
|
||||
`🌐 Using proxy for ${apiMethod}: ${ProxyHelper.getProxyDescription(proxyConfig)}`
|
||||
)
|
||||
logger.info(`🌐 Using proxy for ${apiMethod}: ${ProxyHelper.getProxyDescription(proxyConfig)}`)
|
||||
} else {
|
||||
logger.debug(`🌐 No proxy configured for ${apiMethod}`)
|
||||
}
|
||||
@@ -1109,52 +1107,55 @@ async function loadCodeAssist(client, projectId = null, proxyConfig = null) {
|
||||
|
||||
const { token } = await client.getAccessToken()
|
||||
const proxyAgent = ProxyHelper.createProxyAgent(proxyConfig)
|
||||
// 🔍 只有个人账户(无 projectId)才需要调用 tokeninfo/userinfo
|
||||
// 这些调用有助于 Google 获取临时 projectId
|
||||
if (!projectId) {
|
||||
const tokenInfoConfig = {
|
||||
url: 'https://oauth2.googleapis.com/tokeninfo',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
data: new URLSearchParams({ access_token: token }).toString(),
|
||||
timeout: 15000
|
||||
}
|
||||
|
||||
const tokenInfoConfig = {
|
||||
url: 'https://oauth2.googleapis.com/tokeninfo',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
data: new URLSearchParams({ access_token: token }).toString(),
|
||||
timeout: 15000
|
||||
}
|
||||
if (proxyAgent) {
|
||||
tokenInfoConfig.httpAgent = proxyAgent
|
||||
tokenInfoConfig.httpsAgent = proxyAgent
|
||||
tokenInfoConfig.proxy = false
|
||||
}
|
||||
|
||||
if (proxyAgent) {
|
||||
tokenInfoConfig.httpAgent = proxyAgent
|
||||
tokenInfoConfig.httpsAgent = proxyAgent
|
||||
tokenInfoConfig.proxy = false
|
||||
}
|
||||
try {
|
||||
await axios(tokenInfoConfig)
|
||||
logger.info('📋 tokeninfo 接口验证成功')
|
||||
} catch (error) {
|
||||
logger.warn('⚠️ tokeninfo 接口调用失败:', error.message)
|
||||
}
|
||||
|
||||
try {
|
||||
await axios(tokenInfoConfig)
|
||||
logger.info('📋 tokeninfo 接口验证成功')
|
||||
} catch (error) {
|
||||
logger.info('tokeninfo 接口获取失败', error)
|
||||
}
|
||||
const userInfoConfig = {
|
||||
url: 'https://www.googleapis.com/oauth2/v2/userinfo',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Accept: '*/*'
|
||||
},
|
||||
timeout: 15000
|
||||
}
|
||||
|
||||
const userInfoConfig = {
|
||||
url: 'https://www.googleapis.com/oauth2/v2/userinfo',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Accept: '*/*'
|
||||
},
|
||||
timeout: 15000
|
||||
}
|
||||
if (proxyAgent) {
|
||||
userInfoConfig.httpAgent = proxyAgent
|
||||
userInfoConfig.httpsAgent = proxyAgent
|
||||
userInfoConfig.proxy = false
|
||||
}
|
||||
|
||||
if (proxyAgent) {
|
||||
userInfoConfig.httpAgent = proxyAgent
|
||||
userInfoConfig.httpsAgent = proxyAgent
|
||||
userInfoConfig.proxy = false
|
||||
}
|
||||
|
||||
try {
|
||||
await axios(userInfoConfig)
|
||||
logger.info('📋 userinfo 接口获取成功')
|
||||
} catch (error) {
|
||||
logger.info('userinfo 接口获取失败', error)
|
||||
try {
|
||||
await axios(userInfoConfig)
|
||||
logger.info('📋 userinfo 接口获取成功')
|
||||
} catch (error) {
|
||||
logger.warn('⚠️ userinfo 接口调用失败:', error.message)
|
||||
}
|
||||
}
|
||||
|
||||
// 创建ClientMetadata
|
||||
|
||||
Reference in New Issue
Block a user