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

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營(yíng)銷資訊 > 網(wǎng)站運(yùn)營(yíng) > 網(wǎng)站添加第三方登錄(新浪微博)

網(wǎng)站添加第三方登錄(新浪微博)

時(shí)間:2023-05-23 07:57:02 | 來源:網(wǎng)站運(yùn)營(yíng)

時(shí)間:2023-05-23 07:57:02 來源:網(wǎng)站運(yùn)營(yíng)

網(wǎng)站添加第三方登錄(新浪微博):

概要:

昨天接入了GitHub的第三方登錄,迫不及待想要接入QQ和新浪微博的第三方登錄,由于QQ和微博的三方登錄申請(qǐng)都還在審核中,無法進(jìn)行登錄申請(qǐng),但是今天發(fā)現(xiàn)微博有一個(gè)添加測(cè)試用戶的功能,也就是說可以在還沒有通過審核的前提下進(jìn)行三方登錄的申請(qǐng),但是只有測(cè)試用戶才能登錄成功,其他用戶則不會(huì)返回?cái)?shù)據(jù),所以打算先把代碼寫上,測(cè)試就添加測(cè)試用戶進(jìn)行測(cè)試,當(dāng)審核通過后就可以實(shí)現(xiàn)所有用戶的微博登錄。

OAuth2.0授權(quán)登錄的過程:

1:引導(dǎo)需要授權(quán)的用戶到驗(yàn)證地址,如果用戶同意授權(quán),頁面跳轉(zhuǎn)至你創(chuàng)建應(yīng)用時(shí)填寫的回調(diào)地址,并且會(huì)通過GET的方式返回一個(gè)code參數(shù),這個(gè)參數(shù)后面會(huì)用到。

請(qǐng)求方式和URL:

GET/POST https://api.weibo.com/oauth2/authorize 請(qǐng)求需要的參數(shù):


返回的數(shù)據(jù):

示例:

//請(qǐng)求https://api.weibo.com/oauth2/authorize?client_id=123050457758183&redirect_uri=http://www.example.com/response&response_type=code//同意授權(quán)后會(huì)重定向http://www.example.com/response&code=CODE2:換取Access Token。

請(qǐng)求的方式及URL:

POST https://api.weibo.com/oauth2/access_token請(qǐng)求需要的參數(shù):

返回的數(shù)據(jù):

到這里就已經(jīng)得到了能夠查詢到用戶信息的兩個(gè)重要參數(shù):access_token和uid。

3:請(qǐng)求得到用戶信息(用戶名、用戶頭像等)

請(qǐng)求的方式及URL:

GET https://api.weibo.com/2/users/show.json請(qǐng)求需要的參數(shù):

返回的部分?jǐn)?shù)據(jù):

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

將微博用戶加入到自己的用戶系統(tǒng)的方法與將GitHub用戶加入到自己的用戶系統(tǒng)的方式相同,可以參考添加第三方登錄(GitHub)。
代碼如下:

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) # 用戶微博登錄標(biāo)記 user_weibo_idstr = 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.username# 得到微博idstrdef get_user_weibo_idstr(self): if Profile.objects.filter(user=self).exists(): return Profile.objects.get(user=self).user_weibo_idstr else: return ''def 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() == '') and (self.get_user_weibo_idstr() == ''): # 普通用戶 return self.username elif (self.get_user_github_node_id() != '') and (self.get_user_weibo_idstr() == ''): # GitHub用戶 username = re.findall('([/s/S]*?)node_id', self.username) return username[0] elif (self.get_user_github_node_id() == '') and (self.get_user_weibo_idstr() != ''): # 微博用戶 username = re.findall('([/s/S]*?)idstr', self.username) return username[0] else: return self.usernameUser.get_user_weibo_idstr = get_user_weibo_idstrUser.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

請(qǐng)求數(shù)據(jù)和完成登錄

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

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.# 繞過密碼驗(yàn)證直接登錄用戶系統(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": context = {} context['message'] = 'GitHub回調(diào)內(nèi)容錯(cuò)誤,無法正常登錄' code = request.GET.get('code', '') # 獲得session中的來源頁面 from_page = request.session.get('from_page', '/main/') # 清除session del request.session['from_page'] if code == '': return render(request, 'message.html', context) # 通過requests發(fā)起POST請(qǐng)求,得到對(duì)應(yīng)的access_token hea = { 'Accept': 'application/json' } data = { 'client_id': 'xxxxxxxxxx', 'client_secret': 'xxxxxxxxxxxxxxxxxxxx', 'code': code, } response = requests.post('https://github.com/login/oauth/access_token', data=data, headers=hea) if ('error' in response.json().keys()): return render(request, 'message.html', context) 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對(duì)應(yīng)的用戶是否存在 if not Profile.objects.filter(user_github_node_id=node_id).exists(): # 對(duì)應(yīng)的用戶不存在,則需要?jiǎng)?chuàng)建一個(gè)用戶,為了保證用戶的唯一性,用戶名使用username+node_id的方式創(chuàng)建 user = User(username=(user_info['login'] + 'node_id=' + node_id), password=node_id) user.save() # 創(chuàng)建對(duì)應(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')# 微博登錄的回調(diào)地址def WeiBo_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'] context = {} context['message'] = '回調(diào)信息出現(xiàn)錯(cuò)誤,請(qǐng)重新登錄' if code == '': return render(request, 'message.html', context) # 通過POST請(qǐng)求獲得access_token數(shù)據(jù) get_access_token_data = { 'client_id': 'xxxxxxxxxxxxxxxx', 'client_secret': 'xxxxxxxxxxxxxxxxxxxxxxxxx', 'grant_type': 'authorization_code', 'code': code, 'redirect_uri': 'http://106.14.185.220:8000/Weibo_Login/', } response = requests.post('https://api.weibo.com/oauth2/access_token', data=get_access_token_data) # 獲得返回回來的json數(shù)據(jù)的所有key值 keys = response.json().keys() # 判斷access_token是否有返回回來,如果沒有返回access_token則說明請(qǐng)求發(fā)生錯(cuò)誤 if not ('access_token' in keys): return render(request, 'message.html', context) # 請(qǐng)求成功,得到了access_token # 通過GET請(qǐng)求獲得用戶信息 uid = response.json()['uid'] access_token = response.json()['access_token'] url = 'https://api.weibo.com/2/users/show.json?uid=' + uid + '&access_token=' + access_token response = requests.get(url=url) # 獲得返回的json數(shù)據(jù)的所有key值 keys = response.json().keys() # 判斷是否返回screen_name和idstr if not ('screen_name' in keys and 'idstr' in keys and (response.json()['idstr'] != '')): return render(requset, 'message.html', context) # 得到對(duì)應(yīng)的數(shù)據(jù) screen_name = response.json()['screen_name'] # 得到用戶id(字符串) idstr = response.json()['idstr'] # 得到用戶頭像 user_img = response.json()['profile_image_url'] username = screen_name + 'idstr' + idstr # 通過idstr查詢數(shù)據(jù)庫中是否存在對(duì)應(yīng)的登錄記錄 if not Profile.objects.filter(user_weibo_idstr=idstr).exists(): # 不存在對(duì)應(yīng)的記錄,需要?jiǎng)?chuàng)建對(duì)應(yīng)的記錄 # 創(chuàng)建用戶 weibo_user = User(username=username, password=idstr) weibo_user.save() # 創(chuàng)建對(duì)應(yīng)的登錄記錄 weibo_login = Profile(user=weibo_user, user_img_base64=user_img, user_weibo_idstr=idstr) weibo_login.save() # 得到用戶 # 用戶修改用戶名過后及時(shí)更改用戶信息 if not User.objects.filter(username=username).exists(): update_user = Profile.objects.get(user_weibo_idstr=idstr).use update_user.username = username update_user.save() user = User.objects.get(username=username) # 繞過密碼驗(yàn)證直接登錄 login_direct(request, user) return HttpResponseRedirect(from_page) else: context = {} context['message'] = 'is not GET request' return render(request, 'message.html', context)

最終效果

1:登錄入口


2:登錄界面

3:登錄結(jié)果

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

關(guān)鍵詞:

74
73
25
news

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

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