115 lines
2.4 KiB
Markdown
115 lines
2.4 KiB
Markdown
# OAuth 设置指南
|
||
|
||
## 问题诊断
|
||
|
||
如果遇到 `Error 400: invalid_request` 错误,通常是 OAuth 配置问题。
|
||
|
||
### 常见错误原因
|
||
|
||
1. **NEXTAUTH_URL 格式错误**
|
||
- 包含多余的引号
|
||
- URL 格式不正确
|
||
- 协议不匹配(http vs https)
|
||
|
||
2. **Google OAuth 重定向 URI 配置错误**
|
||
- 重定向 URI 未在 Google Cloud Console 中正确配置
|
||
- 重定向 URI 格式不正确
|
||
|
||
## 解决步骤
|
||
|
||
### 1. 检查环境变量
|
||
|
||
在服务器上运行环境变量检查脚本:
|
||
|
||
```bash
|
||
node scripts/check-env.js
|
||
```
|
||
|
||
### 2. 修复 NEXTAUTH_URL
|
||
|
||
确保 `.env.production` 文件中的 `NEXTAUTH_URL` 格式正确:
|
||
|
||
```bash
|
||
# 正确的格式
|
||
NEXTAUTH_URL="https://recorder.zyj.best"
|
||
|
||
# 错误的格式(包含多余引号)
|
||
NEXTAUTH_URL=""https://recorder.zyj.best""
|
||
```
|
||
|
||
### 3. 配置 Google OAuth
|
||
|
||
#### 在 Google Cloud Console 中:
|
||
|
||
1. 访问 [Google Cloud Console](https://console.cloud.google.com/)
|
||
2. 选择你的项目
|
||
3. 进入 "APIs & Services" > "Credentials"
|
||
4. 编辑你的 OAuth 2.0 客户端 ID
|
||
5. 在 "Authorized redirect URIs" 中添加:
|
||
```
|
||
https://recorder.zyj.best/api/auth/callback/google
|
||
```
|
||
|
||
### 4. 验证配置
|
||
|
||
重新部署应用并测试:
|
||
|
||
```bash
|
||
./deploy.sh
|
||
```
|
||
|
||
### 5. 调试信息
|
||
|
||
如果问题仍然存在,检查应用日志:
|
||
|
||
```bash
|
||
docker logs recorder-app
|
||
```
|
||
|
||
## 环境变量模板
|
||
|
||
```bash
|
||
# Database
|
||
DATABASE_URL="file:./prod.db"
|
||
|
||
# NextAuth.js
|
||
NEXTAUTH_URL="https://recorder.zyj.best"
|
||
NEXTAUTH_SECRET="your-secure-secret-here"
|
||
|
||
# Google OAuth
|
||
GOOGLE_CLIENT_ID="your-google-client-id"
|
||
GOOGLE_CLIENT_SECRET="your-google-client-secret"
|
||
|
||
# AWS S3 Configuration
|
||
AWS_ACCESS_KEY_ID="your-aws-access-key-id"
|
||
AWS_SECRET_ACCESS_KEY="your-aws-secret-access-key"
|
||
AWS_REGION="us-east-1"
|
||
AWS_S3_BUCKET="your-s3-bucket-name"
|
||
```
|
||
|
||
## 故障排除
|
||
|
||
### 错误:redirect_uri 格式错误
|
||
|
||
**原因**:NEXTAUTH_URL 包含多余的引号或格式错误
|
||
|
||
**解决**:
|
||
1. 检查 `.env.production` 文件
|
||
2. 确保 NEXTAUTH_URL 格式正确
|
||
3. 重启应用
|
||
|
||
### 错误:redirect_uri 不在授权列表中
|
||
|
||
**原因**:Google OAuth 重定向 URI 未正确配置
|
||
|
||
**解决**:
|
||
1. 在 Google Cloud Console 中添加正确的重定向 URI
|
||
2. 确保 URI 格式为:`https://your-domain.com/api/auth/callback/google`
|
||
|
||
### 错误:invalid_client
|
||
|
||
**原因**:Google OAuth 凭据错误
|
||
|
||
**解决**:
|
||
1. 检查 GOOGLE_CLIENT_ID 和 GOOGLE_CLIENT_SECRET
|
||
2. 确保凭据与 Google Cloud Console 中的配置匹配 |