# 生产环境 OAuth 配置指南 ## 🚨 问题描述 在生产环境中,Google OAuth 登录出现以下错误: ``` Error 400: invalid_request Request details: redirect_uri=https://"https//recorder.zyj.best%22/callback/google ``` ## 🔍 问题分析 ### 根本原因 1. **重定向 URI 格式错误**:URI 包含了多余的引号和错误的编码 2. **环境变量配置问题**:`NEXTAUTH_URL` 可能没有正确设置 3. **Google Cloud Console 配置问题**:授权重定向 URI 可能不正确 ## ✅ 解决方案 ### 1. 环境变量配置 确保服务器上的 `.env.production` 文件包含正确的配置: ```bash # NextAuth.js - 生产环境配置 NEXTAUTH_URL="https://recorder.zyj.best" NEXTAUTH_SECRET="your-production-nextauth-secret" # Google OAuth - 生产环境配置 GOOGLE_CLIENT_ID="your-google-client-id" GOOGLE_CLIENT_SECRET="your-google-client-secret" ``` ### 2. Google Cloud Console 配置 在 Google Cloud Console 中,确保以下重定向 URI 已正确配置: #### 授权重定向 URI ``` https://recorder.zyj.best/api/auth/callback/google ``` #### JavaScript 来源 ``` https://recorder.zyj.best ``` ### 3. 验证步骤 1. **检查环境变量**: ```bash # 在服务器上检查环境变量 docker exec -it recorder-app env | grep NEXTAUTH ``` 2. **检查 Google Cloud Console**: - 登录 [Google Cloud Console](https://console.cloud.google.com/) - 进入 "APIs & Services" > "Credentials" - 检查 OAuth 2.0 客户端 ID 的配置 3. **重启应用**: ```bash ./deploy.sh ``` ### 4. 调试信息 应用现在包含调试信息,可以在日志中查看重定向过程: ```bash docker logs recorder-app ``` ## 🔧 常见问题 ### 问题 1:重定向 URI 不匹配 **解决方案**:确保 Google Cloud Console 中的重定向 URI 与 `NEXTAUTH_URL` 一致 ### 问题 2:HTTPS 证书问题 **解决方案**:确保域名有有效的 SSL 证书 ### 问题 3:环境变量未生效 **解决方案**:重启 Docker 容器以重新加载环境变量 ## 📝 检查清单 - [ ] `NEXTAUTH_URL` 设置为 `https://recorder.zyj.best` - [ ] Google Cloud Console 中配置了正确的重定向 URI - [ ] 环境变量文件正确加载 - [ ] 应用已重启 - [ ] SSL 证书有效