zoukankan      html  css  js  c++  java
  • 微博绑定用户信息

    1.微博绑定用户接口

    1.1 oauth/urls.py 中添加路由
    urlpatterns = [ 
           path('weibo/binduser/', WeiboUser.as_view()), # /oauth/weibo/callback/ ]
    
    1.2 oauth/views.py 中添加试图函数
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from oauth.models import *
    from django.contrib.auth.hashers import make_password
    
    
    # 微博第三方绑定
    class WeiboUser(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,
                    "role": None,
                    "name": user.nick_name,
                    "username": username,
                    "email": user.email,
                    "token": create_token(user),
                    "type": 0
                }
    
                res_data = {
                    "code": 1000,
                    "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": 1000,
                    "msg": "登陆成功",
                    "data": data
                }
                return Response(res_data)
    
    
  • 相关阅读:
    XP系统无法安装net framework 4.0 解决方法
    StructureMap DI & IoC 工具介绍
    Castle ActiveRecord学习实践(7)级联
    Error.popStackFrame 函数
    抽象泄漏(leaky abstraction)
    [Exception]IIS6:The entry "*" has already been added的解决方法
    ASP.NET 设计模式 读书摘记2
    PHP模块开发(一) PHP环境搭建
    PHP函数HTTP 相关函数
    PHP函数FTP文件传输函数
  • 原文地址:https://www.cnblogs.com/chao460/p/13934741.html
Copyright © 2011-2022 走看看