码迷,mamicode.com
首页 > 其他好文 > 详细

微博绑定用户信息

时间:2020-11-07 15:32:47      阅读:9      评论:0      收藏: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!