三方登录微博

OAuth2.0授权认证

oauth2.0是什么?

​ OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用。

img

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)

微博三方登陆

微博登陆流程图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pJjgzjmX-1609158754439)(./pic/微博三方流程图.png)]

接口说明
OAuth2/authorize请求用户授权Token
OAuth2/access_token获取授权过的Access Token, UID
from urllib.parse import urlencode
import requests
from django.contrib.auth.hashers import make_password
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from code2001B.settings import WEIBO_APP_ID,WEIBO_SECRET_KEY
from oauthapp.models import *
from utils.MyBaseView import create_token
from userapp.models import User
from oauthapp.serializers import OauthUserSer
from utils.MyBaseView import qiniu_token

class WeiBoURL(APIView):
    def post(self,request):

        #拼接一个符合微博规范的URL
        url = 'https://api.weibo.com/oauth2/authorize?'  # 微博授权的url地址
        data = {
            'client_id': WEIBO_APP_ID,  # WEIBO_APP_KEY,
            'response_type': 'code',
            'redirect_uri': 'http://127.0.0.1:8888/oauth/callback/',  # VUE的回调,微博后台授权的回调地址
        }


        #********************************
        weibo_url = url + urlencode(data)
        # ********************************

        #https://api.weibo.com/oauth2/authorize?client_id=xxxx&response_type=code

        return Response({'code': '0', 'msg': '成功', 'data': {'url': weibo_url}})



class WeiBoCallback(APIView):
    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请求去请求微博准备的信息-----requests
        json_weibo_data = requests.post(url=url,data=data).json()
        # *******************************************************


        #提取其中有用的信息UID
        uid = json_weibo_data.get("uid")

        #判断是否获取到UID
        if 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 = 1
        username = 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)

多方是登陆

  • Views.py
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值