• 1.微博回调接口 和绑定user接口


     

    1.1 oauth/views.py 中添加试图函数  

    http://192.168.56.100:8888/oauth/weibo/callback/
    

      

    # 通过vue前端传入的code,微博身份验证
    class OauthWeiboCallback(APIView): permission_classes = (AllowAny,) def post(self,request): code=request.data.get('code') data={ 'client_id': '4164371007', # 创建的应用测试id 'client_secret': 'd4fe5c2583f36b379d786a8f8ce56b8b', # 信息 'grant_type': 'authorization_code', 'code': code, 'redirect_uri': 'http://127.0.0.1:8888/oauth/callback/', } url = 'https://api.weibo.com/oauth2/access_token' data = requests.post(url=url, data=data).json() # 拿取请求的返回结果 access_token = data.get('uid') # 获取到的微博token weibo_uid = data.get('access_token') # 获取到少码用户的id try: oauth_user = OauthUser.objects.get(uid=weibo_uid, oauth_type='1') except Exception as e: oauth_user = None # 返回动作, 登录成功/需要绑定用户 type 0 登录成功, 1, 授权成功, 需要绑定 if oauth_user: # 4. 如果绑定了, 返回token, 登录成功 user = oauth_user.user payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) # jwt_response_payload_handler为user模块定义的jwt返回的信息 data = jwt_response_payload_handler(token, user) data['type'] = '0' # 指定为登录成功 return Response({'code': 0, 'msg': '登录成功', 'data': data}) else: # 5. 如果没绑定, 返回标志, 让前端跳转到绑定页面 return Response({'code': 0, 'msg': '授权成功', 'data': {'type': '1','uid': weibo_uid}})

      

    1.1 oauth/urls.py 中添加路由

    urlpatterns = [
    path('weibo/callback/', views.OauthWeiboCallback.as_view()), # 回调
    /oauth/weibo/callback/
    ]

    绑定用户

    1.1oauth/models.py 中添加用户绑定模型

    class OauthUser(models.Model):
        OAUTHTYPE = (
            ('1', 'weibo'),
            ('2', 'weixin'),
            )
        uid = models.CharField('三方用户id', max_length=64)
        # 三方用户id
        user = models.ForeignKey('user.User', on_delete=models.CASCADE)
        # 本地用户外键,关联User表
        oauth_type = models.CharField('认证类型', max_length=10, choices=OAUTHTYPE)        
    

    1.2 迁移数据库

    python manager.py makemigrations
    python manager.py migrate
    

     

    1.3 oauth/views.py 中添加试图函数 

    class OauthWeiboBindUser(APIView):
        permission_classes = (AllowAny,)
        def post(self,request):
            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':999,'msg':'参数不全'})
            try:
                user=User.objects.filter(username=username).first()
            except Exception as e:
                user=None
            if user:
                print(password, user.password,'11111')
                if user.check_password(password):
                    ou=OauthUser(uid=weibo_uid,user=user,oauth_type='1')
                    ou.save()
                    payload=jwt_payload_handler(user)
                    tokon=jwt_encode_handler(payload)
                    data=jwt_response_payload_handler(tokon,user)
                    data['type'] = '0'  # 指定为登录成功
                    return Response({'code': 0, 'msg': '登录成功', 'data': data})
                else:
                    return Response({'code': 999, 'msg': '密码错误'})
            else:
                # 2. 未注册用户
                # 2.1 生成新用户, 设置用户名密码, 保存, 然后绑定, 返回token, 登录成功
                user = User(username=username)
                user.set_password(password)
                user.save()
                ou = OauthUser(uid=weibo_uid, user=user, oauth_type='1')
                ou.save()
                payload = jwt_payload_handler(user)
                token = jwt_encode_handler(payload)
                data = jwt_response_payload_handler(token, user)
                data['type'] ='0' #指定为登录成功
                return Response({'code':0,'msg':'登录成功','data':data})
    

      

    1.4 oauth/urls.py 中添加路由

    urlpatterns = [
        path('weibo/binduser/', views.OauthWeiboBindUser.as_view()), #
    
    ]
    

      

  • 相关阅读:
    使用webstorm来创建并且运行vue项目详细教程
    Jmeter后置处理器之Json提取器
    Linux常用命令
    Linux安装Mysql
    Linux安装JDK
    redhat6.5安装yum
    Linux安装python
    Linux安装Tomcat
    搞懂Redis协议RESP
    1TB是多大?
  • 原文地址:https://www.cnblogs.com/zhangshijiezsj/p/13789071.html
Copyright © 2020-2023  润新知