国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運營 > 網(wǎng)站添加第三方登錄(GitHub)

網(wǎng)站添加第三方登錄(GitHub)

時間:2023-07-31 23:24:01 | 來源:網(wǎng)站運營

時間:2023-07-31 23:24:01 來源:網(wǎng)站運營

網(wǎng)站添加第三方登錄(GitHub):

概要:

為了使用戶在不注冊本網(wǎng)站的用戶系統(tǒng)的情況下也能夠進行登錄過后的能夠進行的操作,引入了第三方登錄,使得用戶能夠快速的處于登錄狀態(tài),常用的第三方登錄的平臺有QQ、微博、人人、GitHub等,但是QQ、微博需要進行認(rèn)證,今天才提交了認(rèn)證資料,還不知道什么時候能夠?qū)徍送ㄟ^,但是GitHub使用起來就比較方便,只要有GitHub賬號就可以直接申請第三方登錄,不需要認(rèn)證什么的,而且文檔也很好用,下面就來介紹一下GitHub的第三方登錄。

開通GitHub OAuth:

要接入GitHub的第三方登錄平臺,必須要先開通GitHub的OAuth,開通方法:settings -> Developer settings -> OAuth apps -> Register a new application 即可開通一個OAuth應(yīng)用,開通OAuth時需要填寫相應(yīng)的信息,這里比較重要的一個信息就是Authorization callback URL即回調(diào)地址,回調(diào)地址的作用是:當(dāng)你向GitHub發(fā)起相應(yīng)的請求后,GitHub返回對應(yīng)的數(shù)據(jù)的Url。應(yīng)用開通完成過后,GitHub會給你一些數(shù)據(jù),其中最重要的有:client_idclient_secret,這兩個參數(shù)是你在請求第三方登錄過程中需要用到的兩個必須參數(shù)。大致的請求過程如下:

1:用戶被重定向以請求他們的GitHub身份。請求的方式以及URL:

GET https://github.com/login/oauth/authorize必須的參數(shù):client_id,請求返回的參數(shù):code。

2:GitHub將用戶重定向回您的網(wǎng)站,如果用戶接受您的請求,GitHub會使用臨時code代碼參數(shù)以及您在state參數(shù)的上一步中提供的狀態(tài)重定向回您的站點。如果狀態(tài)不匹配,則請求由第三方創(chuàng)建,并且應(yīng)該中止該過程。請求的方式以及URL:

POST https://github.com/login/oauth/access_token必須的參數(shù):client_id、client_secret、code,返回的參數(shù):access_token。

3:使用訪問令牌訪問API,訪問令牌允許您代表用戶向API發(fā)出請求。請求的方式以及URL:

GET https://api.github.com/user?access_token=...必須的參數(shù):access_token,返回的參數(shù):用戶信息。

如何將GitHub用戶加入到自己的用戶系統(tǒng):

經(jīng)過上面的一系列請求,得到的數(shù)據(jù)只是GitHub用戶的基本信息,比如用戶名,頭像,但是并沒有與本網(wǎng)站的用戶系統(tǒng)發(fā)生聯(lián)系,僅僅這樣是無法完成用戶的登錄的,所以必須將GitHub用戶的信息用本網(wǎng)站的用戶系統(tǒng)關(guān)聯(lián)起來,這樣就可以完成用戶的登錄,我這里使用的方法是:新建模型Profile,在該模型中創(chuàng)建一個字段用來保存與GitHub用戶一一對應(yīng)的一個信息,經(jīng)過對返回的信息進行對比,最后得到node_id這個參數(shù)是唯一的,并且Profile這個模型需要用外鍵OneToOneField來關(guān)聯(lián)User這個模型,當(dāng)用戶發(fā)起第三方登錄請求時,得到對應(yīng)的node_id,通過node_id去查詢Profile這個表,看是否存在對應(yīng)的記錄,如果不存在,則需要創(chuàng)建一條記錄,并且同時需要創(chuàng)建一個用戶,為了保證用戶名的唯一性,這里我使用了用戶名+node_id作用用戶名來創(chuàng)建用戶,后面獲取用戶名是再使用正則表達式來獲得正確的用戶名。

from django.db import modelsfrom django.contrib.auth.models import Userimport re# Create your models here.class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name='昵稱') nickname = models.CharField(max_length=20) user_img_base64 = models.TextField(default='') # 用戶GitHub登錄標(biāo)記 user_github_node_id = models.CharField(default='', max_length=100) def __str__(self): return '' % (self.nickname, self.user.username)def get_nickname(self): if Profile.objects.filter(user=self).exists(): profile = Profile.objects.get(user=self) return profile.nickname else: return ''def get_nickname_or_username(self): if Profile.objects.filter(user=self).exists(): profile = Profile.objects.get(user=self) return profile.nickname else: return self.usernamedef has_nickname(self): return Profile.objects.filter(user=self).exists()def get_user_img_base64(self): if Profile.objects.filter(user=self).exists(): return Profile.objects.get(user=self).user_img_base64 else: return ''def get_user_github_node_id(self): if Profile.objects.filter(user=self).exists(): return Profile.objects.get(user=self).user_github_node_id else: return ''# 自定義得到username的方法def new_get_username(self): if self.get_user_github_node_id() == '': # 普通用戶 print('ok') return self.username else: # 其他用戶 username = re.findall('([/s/S]*?)node_id', self.username) return username[0]User.new_get_username = new_get_usernameUser.get_user_github_node_id = get_user_github_node_idUser.get_user_img_base64 = get_user_img_base64User.get_nickname = get_nicknameUser.has_nickname = has_nicknameUser.get_nickname_or_username = get_nickname_or_username

請求數(shù)據(jù)和完成登錄:

請求數(shù)據(jù)的方法前面已經(jīng)提到了,只要按照對應(yīng)的流程來請求數(shù)據(jù),就能夠得到用戶的基本信息,得到信息過后創(chuàng)建對應(yīng)的記錄,最后就需要完成用戶的登錄,但是用戶是第三方登錄,所以就沒有必要設(shè)置登錄密碼,只要能夠得到對應(yīng)的用戶信息,就說明該用戶在GitHub系統(tǒng)是存在的,所以這里需要用到setattr(user, 'backend', 'django.contrib.auth.backends.ModelBackend')來繞過密碼驗證直接登錄。

from django.shortcuts import render# Create your views here.from django.shortcuts import render, HttpResponse, HttpResponseRedirectimport requestsfrom django.contrib.auth.models import Userfrom user.models import Profilefrom django.contrib import auth# Create your views here.# 繞過密碼驗證直接登錄用戶系統(tǒng)def login_direct(request, user): setattr(user, 'backend', 'django.contrib.auth.backends.ModelBackend') auth.login(request, user)# GitHub登錄的回調(diào)地址def GitHub_Login(request): if request.method == "GET": code = request.GET.get('code', '') # 獲得session中的來源頁面 from_page = request.session.get('from_page', '/main/') # 清除session del request.session['from_page'] if code == '': return HttpResponse('GitHub回調(diào)內(nèi)容錯誤,無法正常登錄') # 通過requests發(fā)起POST請求,得到對應(yīng)的access_token hea = { 'Accept': 'application/json' } data = { 'client_id': 'xxxxxxxxxxxx', 'client_secret': 'xxxxxxxxxxxxxxxxxxxxxxxxxxx', 'code': code, } response = requests.post('https://github.com/login/oauth/access_token', data=data, headers=hea) if ('error' in response.json().keys()): return HttpResponse('GitHub回調(diào)內(nèi)容錯誤,無法正常登錄') access_token = response.json()['access_token'] url = 'https://api.github.com/user?access_token=' + access_token user_info = requests.get(url).json() # 得到node_id node_id = user_info['node_id'] # 判斷node_id對應(yīng)的用戶是否存在 if not Profile.objects.filter(user_github_node_id=node_id).exists(): # 對應(yīng)的用戶不存在,則需要創(chuàng)建一個用戶,為了保證用戶的唯一性,用戶名使用username+node_id的方式創(chuàng)建 user = User(username=(user_info['login'] + 'node_id=' + node_id), password=node_id) user.save() # 創(chuàng)建對應(yīng)的node_id記錄 add_node_id = Profile(user=user, user_github_node_id=node_id, user_img_base64=user_info['avatar_url']) add_node_id.save() else: user = Profile.objects.get(user_github_node_id=node_id) user.user_img_base64 = user_info['avatar_url'] user.save() user = User.objects.get(username=(user_info['login'] + 'node_id=' + node_id)) login_direct(request, user) return HttpResponseRedirect(from_page) else: return HttpResponse('is not GET request')

最終效果

1:登錄入口

2:登錄界面

3:登錄結(jié)果

原文地址、我的網(wǎng)站。

關(guān)鍵詞:

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉