这是一个基于Gin框架的Go服务,用于统一接入第三方平台的OAuth2认证,支持Google和Slack平台。
- ✅ Google OAuth2认证(Gmail/Drive)
- ✅ Slack OAuth2认证
- ✅ 内存token管理(开发环境)
- ✅ 可扩展的provider架构
- ✅ RESTful API接口
- ✅ 完整的API文档和测试接口
connector-demo/ ├── main.go # 主程序入口 ├── go.mod # 依赖管理 ├── config/ │ └── config.go # 配置管理 ├── auth/ │ ├── auth.go # OAuth2路由处理 │ └── provider.go # goth提供者配置 ├── connectors/ │ ├── google_connector.go # Google API连接器 │ └── slack_connector.go # Slack API连接器 ├── utils/ │ ├── token_manager.go # token管理器 │ └── oauth_client.go # OAuth2客户端工具 └── README.md # 项目文档 设置必要的环境变量:
# Google OAuth2配置 export GOOGLE_CLIENT_ID="your_google_client_id" export GOOGLE_CLIENT_SECRET="your_google_client_secret" # Slack OAuth2配置 export SLACK_CLIENT_ID="your_slack_client_id" export SLACK_CLIENT_SECRET="your_slack_client_secret" # 可选配置 export REDIRECT_URL="http://localhost:8080" # 回调URL export PORT="8080" # 服务端口go mod tidy go mod downloadgo run main.goGET /- 服务状态和信息GET /health- 健康检查
GET /auth/connect/:platform- 开始OAuth2流程- 平台:
google,slack
- 平台:
GET /auth/callback/:platform- OAuth2回调处理
GET /tokens?user_id={user_id}- 获取用户tokenDELETE /tokens/disconnect/:platform?user_id={user_id}- 断开连接
GET /api/google/test?user_id={user_id}- 测试连接GET /api/google/gmail?user_id={user_id}- 获取Gmail邮件列表GET /api/google/drive?user_id={user_id}- 获取Drive文件列表
GET /api/slack/test?user_id={user_id}- 测试连接GET /api/slack/channels?user_id={user_id}- 获取频道列表GET /api/slack/messages?user_id={user_id}&channel_id={channel_id}- 获取消息列表
GET /debug/tokens- 查看所有token(调试用)
- 访问:
http://localhost:8080/auth/connect/google - 授权后,系统会在控制台打印access_token和refresh_token
- 使用返回的用户ID测试API:
http://localhost:8080/api/google/test?user_id=用户邮箱
- 访问:
http://localhost:8080/auth/connect/slack - 授权后,系统会在控制台打印access_token和refresh_token
- 使用返回的用户ID测试API:
http://localhost:8080/api/slack/test?user_id=用户ID
- 在
auth/provider.go中添加新的provider配置 - 在
connectors/目录下创建新的连接器文件 - 在
main.go的setupRoutes中添加对应的API路由
// 在auth/provider.go中添加 providers = append(providers, notion.New( cfg.NotionClientID, cfg.NotionClientSecret, fmt.Sprintf("%s/auth/callback/notion", cfg.RedirectURL), ))当前使用内存存储token,生产环境建议:
- 实现数据库存储(Redis/PostgreSQL)
- 添加token刷新机制
- 实现用户会话管理
- 当前token存储在内存中,重启服务会丢失
- 请确保回调URL与OAuth应用配置一致
- 开发环境建议使用
http://localhost:8080 - 生产环境请使用HTTPS
创建.env文件:
# Google OAuth2 GOOGLE_CLIENT_ID=your_google_client_id GOOGLE_CLIENT_SECRET=your_google_client_secret # Slack OAuth2 SLACK_CLIENT_ID=your_slack_client_id SLACK_CLIENT_SECRET=your_slack_client_secret # 服务配置 REDIRECT_URL=http://localhost:8080 PORT=8080 GIN_MODE=debugMIT License