8

Python3+Django2.0.4三方社交账号接入Gitee.com(码云)

 2 years ago
source link: https://v3u.cn/a_id_154
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python3+Django2.0.4三方社交账号接入Gitee.com(码云)

首页 - Python/2020-06-01

    码云大家都耳熟能详了,国内版的github,其三方登录的接入方式也完全照搬github经典的OAuth2逻辑。

20200601210626_69709.png

    首先注册码云的账号,随后在应用管理页面 https://gitee.com/oauth/applications,新建三方应用 : 

20200601210615_69527.png

    这里创建之后可以获取到clinet_id和client_screct

20200601210624_68443.png

    图片logo可以不上传,但是应用域名和回调网址必须填写,需要注意的是必须要写完整网址也就是必须带上http或者https

20200601210657_99575.png

    随后的流程可以参照官方文档:https://gitee.com/api/v5/oauth_doc#/

    第一步,通过 浏览器 或 Webview 将用户引导到码云三方认证页面上( GET请求 )

class Gitee(View):

def get(self,request):

return redirect("https://gitee.com/oauth/authorize?client_id=你的应用id&redirect_uri=http://localhost:8000/gitee_back&response_type=code")

    第二步,如果用户授权登录成功,gitee则会通过回调网址将code传递给第三方应用,此时三方应用可以通过code换取access_token

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&code=%s&client_id=你的应用id&redirect_uri=http://localhost:8000/gitee_back&client_secret=你的应用秘钥" % code)
print(r.text)

return HttpResponse("ok")

    最后一步,码云认证服务器成功返回 access_token,三方应用通过 access_token 访问 Open API 使用用户数据。

    这里我们以基础用户信息接口为例子

r = requests.get("https://gitee.com/api/v5/user?access_token=获取到的accesstoken")

print(r.text)

    

20200601210613_26913.png

    总体上没什么难度,需要注意一点,当需要调用openapi的接口时,需要点击授权。

    访问:https://gitee.com/api/v5/swagger#/getV5User

20200601210620_41870.png

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK