微博绑定用户信息

技术文章 1年前 (2020) 完美者
1,943 0

标签:bind   ret   exce   成功   三方   ram   lang   none   绑定   

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)

微博绑定用户信息

标签:bind   ret   exce   成功   三方   ram   lang   none   绑定   

原文地址:https://www.cnblogs.com/chao460/p/13934741.html

版权声明:完美者 发表于 2020-11-07 15:32:47。
转载请注明:微博绑定用户信息 | 完美导航

暂无评论

暂无评论...