微博绑定用户信息
Posted chao460
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微博绑定用户信息相关的知识,希望对你有一定的参考价值。
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)
以上是关于微博绑定用户信息的主要内容,如果未能解决你的问题,请参考以下文章