CodeBuddy 开放平台是一个为第三方开发者提供的完整生态系统,通过开放 API 和 OAuth 2.0 授权框架,让第三方应用能够安全、高效地集成 CodeBuddy 核心功能。
CodeBuddy OAuth 授权页面 - 用户可选择授权方式
npm install
在项目根目录创建 .env 文件:
# OAuth 配置
CLIENT_ID=你的客户端ID
CLIENT_SECRET=你的客户端密钥
REDIRECT_URI=http://localhost:3000/callback
# OAuth Endpoints
AUTHORIZATION_ENDPOINT=https://copilot.tencent.com/oauth2/authorize
TOKEN_ENDPOINT=https://copilot.tencent.com/oauth2/token
USERINFO_ENDPOINT=https://copilot.tencent.com/oauth2/userinfo
# 应用配置
PORT=3000
SESSION_SECRET=你的会话密钥
npm start
打开浏览器访问:http://localhost:3000
1. 用户点击登录 ↓ 2. 重定向到 CodeBuddy 授权页面 https://copilot.tencent.com/oauth2/authorize ↓ 3. 用户授权 ↓ 4. 回调到应用并交换 access_token 使用 application/x-www-form-urlencoded 格式 ↓ 5. 获取用户信息并显示
authUrl.searchParams.append('scope', 'openid');
authUrl.searchParams.append('response_mode', 'query');
const tokenParams = new URLSearchParams({
grant_type: 'authorization_code',
client_id: config.clientId,
client_secret: config.clientSecret,
code: code,
redirect_uri: config.redirectUri
});
axios.post(url, tokenParams.toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
| 端点类型 | URL |
|---|---|
| 授权端点 | https://copilot.tencent.com/oauth2/authorize |
| 令牌端点 | https://copilot.tencent.com/oauth2/token |
| 用户信息端点 | https://copilot.tencent.com/oauth2/userinfo |
服务器会输出详细日志:
🚀 发起授权: https://copilot.tencent.com/oauth2/authorize?... 🔄 交换授权码... ✅ 令牌交换成功!
oauth_test/ ├── server.js # Express 服务器 ├── package.json # 依赖管理 ├── .env # 环境变量配置 └── public/ ├── index.html # 登录页面 ├── profile.html # 用户信息页面 └── styles.css # 样式文件
状态: ✅ 配置完成,可以使用
访问地址: http://localhost:3000