三方登录——新浪微博登陆
OAuth2.0授权认证
oauth2.0是什么?
OAuth(开放授权)是⼀个开放标准,允许⽤户让第三⽅应⽤访问该⽤户在某⼀⽹站上存储的私密的
资源(如照⽚,视频,联系⼈列表),⽽⽆需将⽤户名和密码提供给第三⽅应⽤。
Oauth2.0的四种授权模式?
https://www.cnblogs.com/Innocent-of-Dabber/p/11009811.html
隐式授权模式(Implicit Grant)
授权码授权模式(Authorization code Grant)
密码模式(Resource Owner Password Credentials Grant)
客户端凭证模式(Client Credentials Grant)
实现新浪微博登陆
新浪微博开放平台:https://open.weibo.com/
一、准备工作
注册新浪微博账号
登录微博开放平台:https://open.weibo.com
进入首页。创建网站接入的应用
选择立即接入,进入到创建应用页面
创建应用
基本信息页面
高级信息页面
二、接口调用
查看接口文档
- OAuth2.0授权认证
接口 | 说明 |
---|---|
OAuth2/authorize | 请求用户授权Token |
OAuth2/access_token | 获取授权过的Access Token, UID |
三、使用
微博流程图
#### oauthapp/views.py
from urllib.parse import urlencodeimport requests
from django.contrib.auth.hashers import make_password
from rest_framework.views import APIView
from rest_framework.response import Responsefrom oauthapp.models import OauthUser
from syl_pro.settings import WEIBO_APP_ID, WEIBO_SECRET_KEY# Create your views here.
from userapp.models import User
from utils.MyBaseView import create_tokenclass WeiBoURl(APIView):"""获取微博二维码的url"""def post(self, request):url = 'https://api.weibo.com/oauth2/authorize?' # 微博授权的地址# 携带的参数配置data = {'client_id': WEIBO_APP_ID,'redirect_uri': 'http://127.0.0.1:8888/oauth/callback/',}weibo_url = url + urlencode(data) # https://api.weibo.com/oauth2/authorize?client_id=1584596336&redirect_uri=http%3A%2F%2F127.0.0.1%3A8888%2Foauth%2Fcallback%2F&response_type=codereturn Response({'code': '0', 'msg': '成功', 'data': {'url': weibo_url}})class WeiBoCallback(APIView):'''发送微博返回的code,进行身份验证'''def post(self, request):code = request.data.get("code")data = {'client_id': WEIBO_APP_ID,'client_secret': WEIBO_SECRET_KEY,'grant_type': 'authorization_code','code': code,'redirect_uri': 'http://127.0.0.1:8888/oauth/callback/',}url = "https://api.weibo.com/oauth2/access_token"# *******************************************************# 需要用一个http请求去请求微博准备的信息-----requestsjson_weibo_data = requests.post(url=url, data=data).json()# *******************************************************# 提取其中有用的信息UIDuid = json_weibo_data.get("uid")# 判断是否获取到UIDif uid:try:uid_user = OauthUser.objects.get(uid=uid)res_data = {'code': 0,"msg": "授权成功","data": {"type": "0","uid": uid,"username": uid_user.user.username,"token": create_token(uid_user.user)}}return Response(res_data)except Exception as e:res_data = {'code': 0,"msg": "授权成功","data": {"type": "1","uid": uid,}}return Response(res_data)else:return Response({"code": 999, "msg": "获取微博信息失败"})class WeiBoBindUser(APIView):"""绑定三方登录用户"""def post(self, request):oauth_type = 1username = request.data.get("username")password = request.data.get("password")weibo_uid = request.data.get("weibo_uid")if not all([username, password, weibo_uid]):return Response({"code": 4005, "msg": "参数不全"})# 判断username是否存在try:user = User.objects.get(username=username)oauthinfo = OauthUser.objects.create(uid=weibo_uid, oauth_type=oauth_type,user=user)data = {"authenticated": True,"id": user.id,"a": None,"name": user.nick_name,"username": username,"email": user.email,"token": create_token(user),"type": 0}res_data = {"code": 0,"msg": "登陆成功","data": data}return Response(res_data)except Exception as e:password = make_password(password)user = User.objects.create(username=username, password=password)oauthinfo = OauthUser.objects.create(uid=weibo_uid, oauth_type=oauth_type,user=user)data = {"authenticated": True,"id": user.id,"role": None,"name": user.nick_name,"username": username,"email": user.email,"token": create_token(user),"type": 0}res_data = {"code": 0,"msg": "登陆成功","data": data}return Response(res_data)
###### oauthapp/models.py 创建三方登陆用户关联表
from django.db import models
from userapp.models import User# Create your models here.
class OauthUser(models.Model):OAUTHTYPE = (('1', 'weibo'),('2', 'weixin'),)uid = models.CharField('三方用户id', max_length=64)user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)oauth_type = models.CharField('认证类型', max_length=10, choices=OAUTHTYPE)
-----------------------------------------------------------
###### oauthapp/serializers.py
from django.contrib.auth.hashers import make_password
from rest_framework import serializers
from userapp.models import *
from oauthapp.models import OauthUserclass OauthUserSer(serializers.ModelSerializer):class Meta:model = OauthUserfields = "__all__"depath = 1
多方式登陆
from django.contrib.auth.backends import ModelBackend
class PPAuth(ModelBackend):def authenticate(self, request, username=None, password=None, **kwargs):user = models.User.objects.get(Q(username=username)
Q(phone=username) | Q(email=username))if user is not None and user.check_password(password):return user
ackend):
def authenticate(self, request, username=None, password=None, **kwargs):
user = models.User.objects.get(Q(username=username)
Q(phone=username) | Q(email=username))
if user is not None and user.check_password(password):
return user
三方登录——新浪微博登陆相关推荐
- 三方登录---新浪微博登陆
OAuth2.0授权认证 oauth2.0是什么? OAuth(开放授权)是⼀个开放标准,允许⽤户让第三⽅应⽤访问该⽤户在某⼀⽹站上存储的私密的 资源(如照⽚,视频,联系⼈列表),⽽⽆需将⽤户名和密码 ...
- 三方登录---新浪微博登录
OAuth2.0授权认证 oauth2.0是什么? OAuth(开放授权)是⼀个开放标准,允许⽤户让第三⽅应⽤访问该⽤户在某⼀⽹站上存储的私密的 资源(如照⽚,视频,联系⼈列表),⽽⽆需将⽤户名和密码 ...
- python钉钉扫码登录程序_使用python+django集成钉钉三方扫码登陆
使用python+django集成钉钉三方扫码登陆 \(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\) 1. 进入钉钉开放平台--->点击左下角 ...
- 微博pythonurllib登陆是什么_python模拟登录新浪微博抓取数据(cookielib和urllib2)
http是无连接的状态协议,但是客户端和服务器端需要保持一些相互信息,比如cookie,有了cookie,服务器才能知道刚才是这个用户登录了网站,才会给予客户端访问一些页面的权限. 用浏览器登录新浪微 ...
- Mob ShareSdk 三方登录、三方分享
我记得上一次使用 Mob 还是在 2017年 的时候,当时可能是做了一个分享的功能,一回头都过去5年多了- 没想到新公司的新项目,沿用的是 Mob 提供的 三方登录 和 三方分享 ,有些怀念的感觉,所 ...
- Python爬虫高级之JS渗透登录新浪微博 | 知了独家研究
小伙伴们看到标题可能会想,我能直接自己登陆把登陆后的cookie复制下来加到自定义的请求头里面不香嘛,为什么非要用python模拟登录的过程?如果我们是长期爬取数据,比如每天早上中午和晚上定时爬取新浪 ...
- 三方登录(微博为例)
三方登录介绍 三方登录流程(以微博为例) 1)前端获取认证code 1. 在Vue页面加载时动态发送请求获取微博授权url 2. django收到请求的url后,通过微博应用ID(client_id) ...
- linux上用selenium登录新浪微博,获取用户关注的用户id
环境:ubuntu.hadoop2.7.3 1.安装selenium 命令:sudo pip install selenium 更新:sudo pip install -U selenium 2.查看 ...
- A072_前台登录_三方登录
目录 内容介绍 1. 前台登录-账号登录 1.1.前台登录 1.2.axios携带token-common.js 1.3.axios后置处理后台拦截错误-common.js 1.4.前台登录拦截-co ...
- uni-app---第三方登录
uni-app---第三方登录 使用uniapp开发app,难免涉及到第三方账号登录 使用`uni.log`inAPI实现第三方登录 两个界面 `my.vue` 页面 `login.vue` 页面 解 ...
最新文章
- 7. Python运算符之逻辑、成员、身份运算符及优先级
- ocr识别技术-车牌识别一体机的核心关键
- 计算机在线考试系统的参考文献,基于JSP的在线考试系统
- firefox android 去更新,Android版Firefox Beta发布更新
- 无法添加 WebEx 扩展插件。请重试。
- 【codevs1935】【BZOJ2879】美食节,网络流之动态加点
- 系统学习机器学习之神经网络(四) --SOM
- 嵌入式系统——软件开发模型
- android第一个项目HelloWorld的搭建
- tms xdata、sparkle服务运行时显示拒绝访问的错误
- html5 按钮效果,7款外观迷人的HTML5/CSS3 3D按钮特效
- FusionStorage原理及组件
- linux下root权限管理账号
- python转xlsx为xls 或重新保存xls
- python的复数的实部虚部都是浮点数吗_python中复数的共轭复数知识点总结
- Excel中数据透视表的使用(一)
- 移动地图定位软件完成了
- 每个工程师都应该知道的 5 个射频发射器测量指标(自NI官网翻译)
- android 中止应用程序,如何终止Xamarin应用程序?
- 基于PHP后台的购物商城微信小程序的设计与实现 毕业设计毕设参考