From cd24fe93b7a3ffd17771d67a9771a89cc3271b9c Mon Sep 17 00:00:00 2001 From: wood chen Date: Fri, 21 Feb 2025 17:09:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=E9=94=99=E8=AF=AF=E6=9B=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/README.md b/README.md index afa4ee4..7b5279a 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,58 @@ Q58论坛网址: https://q58.club 2. 未登录本系统, 未登录q58论坛, 检查: 用户在接入应用中登录, 然后登录本系统, 然后登录q58论坛, 正常一直回调到用户应用 3. 未登录本系统, 登录了q58论坛, 检查: 用户在接入应用中登录, 然后登录本系统, 正常回调到用户应用 +## 用户接入本系统oauth2.0认证的方式: + +1. 发起授权请求 + 将用户重定向到授权页面 + +const authUrl = 'https://connect.q58.club/oauth/authorize?' + +new URLSearchParams({ +response_type: 'code', // 必填,固定值 +client_id: 'your_client_id', // 必填,您的应用ID +redirect_uri: 'https://your-app.com/callback', +state: 'random_state', // 建议提供,防CSRF攻击 +scope: 'read_profile' // 可选,默认read_profile +}); + +window.location.href = authUrl; + +2. 处理授权回调 + 在回调地址处理授权结果 + +// 获取访问令牌 +const response = await fetch('https://connect.q58.club/api/oauth/access_token', { +method: 'POST', +headers: { +'Content-Type': 'application/x-www-form-urlencoded' +}, +body: new URLSearchParams({ +code: '授权码', // 回调参数中的code +redirect_uri: 'https://your-app.com/callback' +}) +}); + +const { access_token, expires_in } = await response.json(); + +3. 获取用户信息 + 使用访问令牌获取用户数据 + +const userInfo = await fetch('https://connect.q58.club/api/oauth/user', { +headers: { +'Authorization': `Bearer ${access_token}` +} +}).then(res => res.json()); + +// 返回数据示例: +{ +"id": "user_xxx", +"email": "user@example.com", +"username": "username", +"name": "用户昵称", +"avatar_url": "https://...", +"groups": ["group1", "group2"] +} + ## 许可证 本项目采用 MIT 许可证。详情请见 [LICENSE](LICENSE) 文件。