Gitee三方登录_Python (超详细)
<p><strong>第三方登录是一种常见的身份验证机制,允许用户使用他们在其他平台(如社交媒体、电子邮件服务或开发平台)的账号来登录你的应用或网站,而不需要创建新的用户名和密码。这种方式不仅简化了用户的登录过程,还提高了用户体验和安全性。</strong><br>第三方登录的主要特点<br>
简化注册和登录:<br>
用户无需创建新的账户,只需使用已有的第三方账户即可快速登录。<br>
减少了用户忘记密码和管理多个账户的问题。<br>
提高安全性:<br>
用户的密码不会存储在你的系统中,减少了密码泄露的风险。<br>
第三方平台通常有更严格的安全措施来保护用户数据。<br>
增加用户信任:<br>
用户对知名第三方平台的信任度较高,使用这些平台的账号登录可以增加用户对你的应用或网站的信任</p>
<h4 id="第三方登录gitee">第三方登录(Gitee)</h4>
<ol>
<li>
<p>注册应用<br>
在Gitee开发者平台注册一个应用,获取client_id和client_secret<br>
配置回调地址(callback url),这是用户授权后将被重定向到的地址</p>
</li>
<li>
<p>引导用户到授权页面<br>
构建一个授权请求url,引导用户到gitee的授权页面<br>
请求url格式如下</p>
<pre><code class="language-python">https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code
#client_id: 你的应用 ID。
#redirect_uri: 回调地址,必须与应用配置中的回调地址一致。
#response_type: 响应类型,通常是 code。
#scope: 请求的权限范围,例如 user_info。
</code></pre>
</li>
<li>
<p>用户授权<br>
用户点击链接后会被重定向到gitee的授权页面<br>
用户选择是否授权你的应用访问其数据<br>
如果用户同意授权,gitee将会重定向回你配置的回调地址,并附带一个授权码(code)</p>
</li>
<li>
<p>获取访问令牌<br>
使用授权码(code)向gitee发送请求,获取访问令牌(access_token)<br>
请求url格式如下</p>
<pre><code class="language-python">https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
</code></pre>
</li>
<li>
<p>使用访问令牌<br>
获取到access_token后,你可以使用它来访问gitee api获取用户信息或其它资源<br>
例如获取用户信息url</p>
<pre><code class="language-python">https://gitee.com/api/v5/user?access_token=7be75844c5439749f367c27cdbb96790
</code></pre>
</li>
</ol>
<h3 id="新建应用">新建应用</h3>
<p><img src="https://img2024.cnblogs.com/blog/1737767/202412/1737767-20241217113126410-161657756.png" alt="" loading="lazy"></p>
<h3 id="gitee-oauth">gitee oauth</h3>
<p><img src="https://img2024.cnblogs.com/blog/1737767/202412/1737767-20241217113209582-1154509863.png" alt="" loading="lazy"></p>
<p>OAuth2 获取 AccessToken 认证步骤<br>
** 授权码模式**<br>
应用通过 浏览器 或 Webview 将用户引导到码云三方认证页面上( GET请求 )</p>
<pre><code class="language-python">https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code
``
用户对应用进行授权
注意: 如果之前已经授权过的需要跳过授权页面,需要在上面第一步的 URL 加上 scope 参数,且 scope 的值需要和用户上次授权的勾选的一致。如用户在上次授权了user_info、projects以及pull_requests。则步骤A 中 GET 请求应为:
```python
https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope=user_info%20projects%20pull_requests
</code></pre>
<p>码云认证服务器通过回调地址{redirect_uri}将 用户授权码 传递给 应用服务器 或者直接在 Webview 中跳转到携带 用户授权码的回调地址上,Webview 直接获取code即可({redirect_uri}?code=abc&state=xyz)<br>
应用服务器 或 Webview 使用 access_token API 向 码云认证服务器发送post请求传入 用户授权码 以及 回调地址( POST请求 )<br>
注:请求过程建议将 client_secret 放在 Body 中传值,以保证数据安全。</p>
<pre><code class="language-python">https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
</code></pre>
<p>码云认证服务器返回 access_token<br>
应用通过 access_token 访问 Open API 使用用户数据。<br>
当 access_token 过期后(有效期为一天),你可以通过以下 refresh_token 方式重新获取 access_token( POST请求 )</p>
<pre><code class="language-python">https://gitee.com/oauth/token?grant_type=refresh_token&refresh_token={refresh_token}
</code></pre>
<p>注意:如果获取 access_token 返回 403,可能是没有设置User-Agent的原因。</p>
<h3 id="工具类">工具类</h3>
<pre><code class="language-python">third_login = {
'gitee':'Gitee'
}
# 封装工厂类
class SimpleFactory:
@staticmethod
def product(name):
return eval(third_login+"()")
class Gitee:
def __init__(self):
self.redirect_uri = 'http://localhost:5000/gitee_back/'
self.client_id = 'db129dabb36711081dc7273f1cb174d051a68eb4f8e041ecda32b7d2dcb60203'
self.client_secret = '7d4d60b0c9b2d89e04ddf802a32a103768976143f4ede832645b7bc442cbf7ed'
# token
self.token_api = 'https://gitee.com/oauth/token'
#获取三方登录链接
def get_url(self):
return f"https://gitee.com/oauth/authorize?client_id={self.client_id}&redirect_uri={self.redirect_uri}&response_type=code"
# 获取用户信息
def get_info(self, code):
# 换token
res = requests.post(
f"https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={self.client_id}&redirect_uri={self.redirect_uri}&client_secret={self.client_secret}")
token = res.json()["access_token"]
# 获取用户信息
res = requests.get(f"https://gitee.com/api/v5/user?access_token={token}")
name = res.json()["name"]
avatar_url = res.json()["avatar_url"]
return name,avatar_url
</code></pre>
<h3 id="视图层">视图层</h3>
<pre><code class="language-python">
# 回调
class GiteeBack(MethodView):
# 回调接口
def get(self):
code = request.args.get('code', None)
gitee = SimpleFactory.product('gitee')
data = gitee.get_info(code)
return jsonify({'code':Code.OK,'msg':code_desc,'data':data})
# 返回url
def post(self):
return jsonify({'code':Code.OK,'msg':code_desc,'url':SimpleFactory.product('gitee').get_url()})
</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/1737767/202412/1737767-20241217113715599-43078193.png" alt="" loading="lazy"></p>
<p><img src="https://img2024.cnblogs.com/blog/1737767/202412/1737767-20241217113733041-1589169638.png" alt="" loading="lazy"></p>
<p><img src="https://img2024.cnblogs.com/blog/1737767/202412/1737767-20241217113747052-1337093732.png" alt="" loading="lazy"></p><br><br>
来源:https://www.cnblogs.com/luckyletop/p/18612003
頁:
[1]