QQ登陆,亦即咱们所说的第三方登陆,是指用户能够不在本项目中输入密码,而直接经过第三方的验证,成功登陆本项目。html
若想实现QQ登陆,须要成为QQ互联的开发者,审核经过才可实现。注册方法可参考连接http://wiki.connect.qq.com/%E6%88%90%E4%B8%BA%E5%BC%80%E5%8F%91%E8%80%85python
成为QQ互联开发者后,还需建立应用,即获取本项目对应与QQ互联的应用ID,建立应用的方法参考连接http://wiki.connect.qq.com/__trashed-2ios
QQ登陆开发文档链接http://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-0数据库
QQ登陆流程图django
建立一个新的应用oauth,用来实现QQ第三方认证登陆。总路由前缀 oauth/json
建立应用axios
注册应用ide
注册路由工具
再建立qq模型类以前,咱们来搞一个基类:测试
from django.db import models class BaseModel(models.Model): """补充字段""" create_time = models.DateTimeField(auto_now_add=True, verbose_name='建立时间') update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间') class Meta: # 说明是抽象模型类,用于继承使用,数据库迁移时不会建立BaseModel的表 abstract = True
而后建立qq模型类:
from django.db import models # Create your models here. from md_mall.utils.models import BaseModel class OAuthQQUser(BaseModel): """ QQ登陆用户数据 """ user = models.ForeignKey('users.User', on_delete=models.CASCADE, verbose_name='用户') openid = models.CharField(max_length=64, verbose_name='openid', db_index=True) class Meta: db_table = 'tb_oauth_qq' verbose_name = 'QQ登陆用户数据' verbose_name_plural = verbose_name
数据库迁移
python manage.py makemigrations
python manage.py migrate
接下来处理第一步:点击qq登陆以后,要跳转到扫描登陆界面,而咱们如今就须要来获取一下扫描登陆界面的地址。
接口设计
在配置文件中添加关于QQ登陆的应用开发信息
# QQ登陆参数 QQ_CLIENT_ID = '101474184' QQ_CLIENT_SECRET = 'c6ce949e04e12ecc909ae6a8b09b637c' QQ_REDIRECT_URI = 'http://www.meiduo.site:8080/oauth_callback.html' QQ_STATE = '/index.html'
新建oauth/utils.py文件,建立QQ登陆辅助工具类
import json from urllib.request import urlopen import logging from django.conf import settings import urllib.parse from itsdangerous import TimedJSONWebSignatureSerializer as TJWSSerializer, BadData from .exceptions import OAuthQQAPIError from . import constants logger = logging.getLogger('django') class OAuthQQ(object): """ QQ认证辅助工具类 """ def __init__(self, client_id=None, client_secret=None,redirect_uri=None, state=None): self.client_id = client_id or settings.QQ_CLIENT_ID self.client_secret = client_secret or settings.QQ_CLIENT_SECRET self.redirect_uri = redirect_uri or settings.QQ_REDIRECT_URI self.state = state or settings.QQ_STATE def get_login_url(self): url = 'https://graph.qq.com/oauth2.0/authorize?' params = { 'response_type': 'code', 'client_id': self.client_id, 'redirect_uri': self.redirect_uri, 'state': self.state } url += urllib.parse.urlencode(params) return url
在oauth/view.py中实现
class QQAuthURLView(APIView): """ 获取QQ登陆的URL """ def get(self, request): # 获取next参数 next = request.query_params.get('next') # 获取QQ登陆的网址 oauth_qq = OAuthQQ(state=next) login_url = oauth_qq.get_login_url() # 返回 return Response({'login_url': login_url})
修改login.js,在methods中增长qq_login方法
// qq登陆 qq_login: function(){ var next = this.get_query_string('next') || '/'; axios.get(this.host + '/oauth/qq/authorization/?next=' + next, { responseType: 'json' }) .then(response => { location.href = response.data.login_url; }) .catch(error => { console.log(error.response.data); }) }
测试