diff --git a/app/api/calendar/route.ts b/app/api/calendar/route.ts index 5f64bec..9fcbad4 100644 --- a/app/api/calendar/route.ts +++ b/app/api/calendar/route.ts @@ -15,20 +15,32 @@ function getAuth(readonly = true) { ? ['https://www.googleapis.com/auth/calendar.readonly'] : ['https://www.googleapis.com/auth/calendar'] + // 1) Предпочтительно — base64. docker --env-file не парсит кавычки и не + // поддерживает многострочные значения, а private_key в JSON ломается + // на переводах строк → base64 это решает (одна строка ASCII). + const saB64 = process.env.GOOGLE_SA_JSON_B64 + if (saB64) { + try { + const sa = JSON.parse(Buffer.from(saB64, 'base64').toString('utf-8')) + return new google.auth.GoogleAuth({ credentials: sa, scopes }) + } catch (e) { + console.error('[calendar] GOOGLE_SA_JSON_B64 decode failed:', e) + } + } + // 2) Сырая JSON-строка (legacy). const saJson = process.env.GOOGLE_SA_JSON if (saJson) { - const sa = JSON.parse(saJson) - return new google.auth.GoogleAuth({ - credentials: sa, - scopes, - }) + try { + const sa = JSON.parse(saJson) + return new google.auth.GoogleAuth({ credentials: sa, scopes }) + } catch (e) { + console.error('[calendar] GOOGLE_SA_JSON parse failed:', e) + } } + // 3) Fallback на файл (для локальной разработки). const saPath = path.join(process.cwd(), 'google-sa.json') if (fs.existsSync(saPath)) { - return new google.auth.GoogleAuth({ - keyFile: saPath, - scopes, - }) + return new google.auth.GoogleAuth({ keyFile: saPath, scopes }) } return null }