番茄的理想 發表於 2023-8-13 21:09:00

集成gitee第三方登录

<h2 id="第三方登录的原理">第三方登录的原理</h2>
<p>所谓第三方登录,实质就是 OAuth 授权。用户想要登录 A 网站,A 网站让用户提供第三方网站的数据,证明自己的身份。获取第三方网站的身份数据,就需要 OAuth 授权。</p>
<p>举例来说,A 网站允许 Gitee 登录,背后就是下面的流程。</p>
<ul>
<li>A 网站让用户跳转到 Gitee。</li>
<li>Gitee 要求用户登录,然后询问"A 网站要求获得 xx 权限,你是否同意?"</li>
<li>用户同意,Gitee 就会重定向回 A 网站,同时发回一个授权码。</li>
<li>A 网站使用授权码,向 Gitee 请求令牌。</li>
<li>Gitee 返回令牌.</li>
<li>A 网站使用令牌,向 Gitee 请求用户数据。</li>
</ul>
<h2 id="gitee中创建应用">gitee中创建应用</h2>
<ul>
<li>设置中点击【第三方应用】<br>
<img src="https://huihuiteresa.github.io/image/youdao/20230810001.png" alt="aa" loading="lazy"></li>
<li>点击【创建应用】<br>
<img src="https://huihuiteresa.github.io/image/youdao/20230810002.png" alt="aa" loading="lazy"></li>
<li>填写信息,然后点击【创建应用】<br>
<img src="https://huihuiteresa.github.io/image/youdao/20230810003.png" alt="aa" loading="lazy"></li>
</ul>
<h2 id="浏览器跳转gitee">浏览器跳转gitee</h2>
<p>写了一个html页面,只有一个链接按钮:<br>
<img src="https://huihuiteresa.github.io/image/youdao/20230810004.png" alt="aa" loading="lazy"></p>
<p>链接的跳转地址为:</p>
<pre><code>https://gitee.com/oauth/authoriclient_id=d898004fd16f0f0552578023587e98106d3f5b738e956fea31792406bf7a457d&amp;redirect_uri=http://localhost:5000/giteeredirect.html&amp;response_type=code
</code></pre>
<p>点击链接后,跳转到【OAuth 授权请求】页面:<br>
<img src="https://huihuiteresa.github.io/image/youdao/20230810005.png" alt="aa" loading="lazy"></p>
<p>点击【同意授权】后,跳转到redirect_uri指定的地址,并且带有授权码:</p>
<pre><code>http://localhost:5000/giteeredirect.html?code=7396b4fbfeb3ba669827a75421b528a90a2b6a5542f887ad8ea33187dd6bf819
</code></pre>
<p><img src="https://huihuiteresa.github.io/image/youdao/20230810006.png" alt="aa" loading="lazy"></p>
<h2 id="根据授权码获取accesstoken">根据授权码获取accesstoken</h2>
<p>获取token的地址为:</p>
<pre><code>https://gitee.com/oauth/token?grant_type=authorization_code&amp;code={code}&amp;client_id={client_id}&amp;redirect_uri={redirect_uri}&amp;client_secret={client_secret}
</code></pre>
<p>这里直接用postman请求:</p>
<p><img src="https://huihuiteresa.github.io/image/youdao/20230810007.png" alt="aa" loading="lazy"></p>
<h2 id="根据token获取用户信息">根据token获取用户信息</h2>
<p>接口地址为:https://gitee.com/api/v5/user</p>
<p>使用postman请求如下:<br>
<img src="https://huihuiteresa.github.io/image/youdao/20230810008.png" alt="aa" loading="lazy"></p>
<p>gitee文档地址:https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoStargazers?ex=no</p>
<h2 id="参考">参考</h2>
<ul>
<li>https://www.ruanyifeng.com/blog/2019/04/github-oauth.html</li>
<li>https://www.cnblogs.com/lenny-z/p/15738047.html</li>
</ul><br><br>
来源:https://www.cnblogs.com/huiteresa/p/17627289.html
頁: [1]
查看完整版本: 集成gitee第三方登录