按照步骤完成应用配置,获取集成凭证
将以下环境变量添加到你的项目 .env 文件中:
# OAuth 应用凭证 CLIENT_ID=app_7f3k9x2m1p5w8q4n CLIENT_SECRET=sk_a9c2e5g8j1m4p7s0v3x6z # 回调与认证端点 REDIRECT_URL=https://your-app.com/callback AUTH_ENDPOINT=https://auth.example.com TOKEN_ENDPOINT=https://auth.example.com/oauth/token
选择你的技术栈,复制对应的集成代码:
// 1. 构建授权 URL,引导用户登录 const authUrl = new URL('https://auth.example.com/authorize'); authUrl.searchParams.set('client_id', process.env.CLIENT_ID); authUrl.searchParams.set('redirect_uri', process.env.REDIRECT_URL); authUrl.searchParams.set('response_type', 'code'); authUrl.searchParams.set('scope', 'openid profile'); // 2. 用户授权后,使用 code 换取 token const response = await fetch('https://auth.example.com/oauth/token', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: new URLSearchParams({ grant_type: 'authorization_code', code: authorizationCode, client_id: process.env.CLIENT_ID, client_secret: process.env.CLIENT_SECRET, redirect_uri: process.env.REDIRECT_URL, }), }); const { access_token, refresh_token } = await response.json();
应用已准备就绪,你可以继续以下操作: