微博登录

在这里插入图片描述
1 获取到 uid 以后
2 去三方表中判断用户是否存在绑定
3 如果存在 未绑定 则绑定
4 不存在 创建 绑定

代码操作?

1 先在微博开放平台创建应用,然后创建回调地址。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

from urllib.parse import urlencode

import requests
from django.contrib.auth.hashers import make_password
from rest_framework.response import Response
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated, AllowAny, IsAdminUser

from userapp.models import *
from utils.myBaseview import create_token
from utils.myBaseview import qiniu_token


# Create your views here.


# 第一步获取url
class WeiBoUrl(APIView):
    permission_classes = [AllowAny]

    def post(self, request):
        url = 'https://api.weibo.com/oauth2/authorize?'  # 微博地址
        data = {
            'client_id': '1220008183',  # 微博身份标识
            'redirect_uri': 'http://127.0.0.1:8888/oauth/callback/',  # 回掉前段位置
        }
        # 路由拼接
        weibo_url = url + urlencode(data)
        return Response({"code": "0", "msg": "成功", "data": {"url": weibo_url}})


# 拿取code码 去换token 与uid
class WeiBocallback(APIView):
    permission_classes = [AllowAny]

    def post(self, request):
        code = request.data.get("code")
        print(code)
        data = {
            'client_id': '1220008183',
            'client_secret': '48e95b358672a310165430b8d5fa2a75',
            'grant_type': "authorization_code",
            'code': code,
            'redirect_uri': "http://127.0.0.1:8888/oauth/callback/",
        }
        url = 'https://api.weibo.com/oauth2/access_token'
        res = requests.post(url=url, data=data).json()  # code??????
        print("iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii")
        print(res)
        uid = res.get("uid")
        print("uuuuuuuuuu")
        print(uid)
        if uid:
            try:
                outh_obj = OauthUser.objects.get(uid=uid)
                user = outh_obj.user
                data = create_token(user)
                print("1222222222222222222222")
                print(data)
                res_data = {
                    'code': 0, "msg": "授权成功",
                    "data": {
                        "type": 0,
                        "uid": uid,
                        "token": data,
                        "username": outh_obj.user.username
                    }
                }
                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 Weibobanduser(APIView):
    permission_classes = [AllowAny]

    def post(self, request):
        oauth_type = 1
        username = request.data.get("username")
        password = request.data.get("password")
        weibo_uid = request.data.get("weibo_uid")
        print(username, password, weibo_uid)
        if not all([username, password, weibo_uid]):
            return Response({"code": 4005, "msg": "参数不全"})
        try:
            user = User.objects.get(username=username)
            a1 = OauthUser.objects.create(uid=weibo_uid, oauth_type=oauth_type, user=user)
            print("xbxbxbxbxbxbbxbxbbxb")
            print(a1)
            data = {
                "code": 200,
                "msg": "成功",
                "data": {
                    "authenticated": True,
                    "id": user.id,
                    'role': None,
                    "username": username,
                    "token": create_token(user),
                    "type": 0
                }
            }
            return Response(data)
        except:
            user_obj = User.objects.create(username=username, password=make_password(password))
            b1 = OauthUser.objects.create(uid=weibo_uid, oauth_type=oauth_type, user=user_obj)
            print("-------------------------------------------------------")
            print(b1)
            data = {
                "code": 200,
                "msg": "成功",
                "data": {
                    "authenticated": True,
                    "id": user_obj.id,
                    'role': None,
                    "username": username,
                    "token": create_token(user_obj),
                    "type": 0
                }
            }
        print(data, '-------')
        return Response(data)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值