文旅影视 發表於 2020-12-20 19:27:00

python +django 实现码云(gitee)三方登陆

<h1 id="python-django-实现码云gitee三方登陆">python +django 实现码云(gitee)三方登陆</h1>
<hr>
<p>参考博客:https://v3u.cn/a_id_154<br>
https://www.cnblogs.com/anle123/p/13446182.html</p>
<p>gitee开发文档:https://gitee.com/api/v5/oauth_doc#/list-item-1</p>
<p>官网地址:https://gitee.com/</p>
<h3 id="oauth2-认证基本流程">OAuth2 认证基本流程</h3>
<p><img src="https://img2020.cnblogs.com/blog/2166327/202012/2166327-20201220192622354-1993730694.png" alt="" loading="lazy"></p>
<h3 id="首先注册码云的账号并且新建三方应用">首先注册码云的账号,并且新建三方应用</h3>
<h6 id="a--点击自己的头像进入设置页面">a.点击自己的头像进入设置页面</h6>
<h6 id="b点击新建三方应用">b.点击新建三方应用</h6>
<p><img src="https://img2020.cnblogs.com/blog/2166327/202012/2166327-20201220192631276-500915635.png" alt="" loading="lazy"></p>
<p><strong><span class="math inline">\(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\)</span></strong></p>
<h3 id="3填写应用相关信息勾选应用所需要的权限其中-回调地址是用户授权后码云回调到应用并且回传授权码的地址">3.填写应用相关信息,勾选应用所需要的权限。其中: 回调地址是用户授权后,码云回调到应用,并且回传授权码的地址</h3>
<p><img src="https://img2020.cnblogs.com/blog/2166327/202012/2166327-20201220192708394-2119308559.png" alt="" loading="lazy"></p>
<p><strong><span class="math inline">\(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\)</span></strong></p>
<p><strong>应用主页</strong>:要求不严格,测试用的话可以直接填<code>http://127.0.0.1:8000/</code></p>
<p><strong>应用回调地址</strong> :这里要填写自己定义的视图路由,我自己的为<code>http://127.0.0.1:8000/gitee_back</code></p>
<p><strong><span class="math inline">\(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\)</span></strong></p>
<h4 id="4创建成功后会生成-cliend-id-和-client-secret他们将会在上述oauth2-认证基本流程用到">4.创建成功后,会生成 Cliend ID 和 Client Secret。他们将会在上述OAuth2 认证基本流程用到</h4>
<p><img src="https://img2020.cnblogs.com/blog/2166327/202012/2166327-20201220192640209-1182479512.png" alt="" loading="lazy"></p>
<h3 id="vue端代码">vue端代码</h3>
<p>我们这里直接使用window.location.href = url;进行调转。</p>
<pre><code class="language-javascript">这里只写一个点击方法

//gitee登陆
gitee:function(){
    //创建应用后生成的Cliend ID
    var clientId = '*********************************'
    //应用回调地址
    var redirect_uri = 'http://127.0.0.1:8000/gitee_back'
    //拼接要请求的地址
    var url = 'https://gitee.com/oauth/authorize?client_id='+clientId+'&amp;redirect_uri='+redirect_uri+'&amp;response_type=code'
    // 进行跳转
    window.location.href = url;
},
</code></pre>
<p><strong><span class="math inline">\(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\)</span></strong></p>
<p>随后的流程可以参照官方文档:https://gitee.com/api/v5/oauth_doc#/</p>
<h3 id="django代码">django代码</h3>
<h5 id="第一步通过-浏览器-或-webview-将用户引导到码云三方认证页面上-get请求-">第一步,通过 浏览器 或 Webview 将用户引导到码云三方认证页面上( GET请求 )</h5>
<pre><code class="language-python">class Gitee(View):

    def get(self,request):

      return redirect("https://gitee.com/oauth/authorize?client_id=你的应用id&amp;redirect_uri=http://localhost:8000/gitee_back&amp;response_type=code")
</code></pre>
<p><strong><span class="math inline">\(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\)</span></strong></p>
<h4 id="第二步如果用户授权登录成功gitee则会通过回调网址将code传递给第三方应用此时三方应用可以通过code换取access_token">第二步,如果用户授权登录成功,gitee则会通过回调网址将code传递给第三方应用,此时三方应用可以通过code换取access_token</h4>
<pre><code class="language-python">class GiteeBack(View):

    def get(self,request):

      code = request.GET.get("code",None)

      r = requests.post("https://gitee.com/oauth/token?grant_type=authorization_code&amp;code=%s&amp;client_id=你的应用id&amp;redirect_uri=http://localhost:8000/gitee_back&amp;client_secret=你的应用秘钥" % code)
      print(r.text)

      return HttpResponse("ok")
</code></pre>
<h4 id="_"><strong><span class="math inline">\(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\)</span></strong></h4>
<h4 id="这里我们以基础用户信息接口为例子">这里我们以基础用户信息接口为例子</h4>
<pre><code class="language-python">r = requests.get("https://gitee.com/api/v5/user?access_token=获取到的accesstoken")

print(r.text)
</code></pre>
<p>最后我们会得到一个用户信息。</p>
<p><strong><span class="math inline">\(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\)</span></strong></p>
<p><strong>总结</strong>:</p>
<p>​        用户通过前端点击gitee登陆图标,跳转到gitee授权页面点击授权我们会获取到用户token,通过token去请求换取用户身份信息</p>


</div>
<div id="MySignature" role="contentinfo">
    从小白到大神的蜕变~~<br><br>
来源:https://www.cnblogs.com/tjw-bk/p/14164670.html
頁: [1]
查看完整版本: python +django 实现码云(gitee)三方登陆