2020/11/05:三方登录
思路整理:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/587f85b8dc904bdcb682f28838d94d73.png#pic_center)
-
创建第三方app:![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e61c79759a914350402bba1d6d03bb4d.png#pic_center)
-
创建表:
from django.db import models
from users.models import User
class OauthUser(models.Model):
OAUTHTYPE = (
('1', 'weibo'),
('2', 'weixin'),
)
uid = models.CharField('三方用户ID', max_length=64)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
oauth_type = models.CharField('认证类型', max_length=10, choices=OAUTHTYPE)
class WeiBoView(APIView):
def post(self, request):
url = 'https://api.weibo.com/oauth2/authorize?'
data = {
'client_id': "977471988",
'response_type': 'code',
'redirect_uri': 'http://127.0.0.1:8888/oauth/callback',
}
weibo_url = url + urlencode(data)
return Response({
'code': 200,
'msg': '成功!',
'data': {
'url': weibo_url
}
})
class WeiBoCallback(APIView):
def post(self, request):
code = request.data.get('code')
print(code, "-------------------------post!")
data = {
'client_id': '977471988',
'client_secret': 'e31ef496080542c04bdf5a7ea92c2f6b',
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': 'http://127.0.0.1:8888/oauth/callback',
}
url = 'https://api.weibo.com/oauth2/access_token'
weibo_data = requests.post(url=url, data=data)
print(type(weibo_data))
json_weibo_data = weibo_data.json()
print(json_weibo_data)
uid = json_weibo_data.get('uid')
if uid:
try:
oauth_obj = OauthUser.objects.get(uid=uid)
return Response({
'code': 200,
'msg': "授权成功!",
'data': {
"uid": uid,
'type': "0",
'username': oauth_obj.username,
'token': create_token(oauth_obj.user)
}
})
except Exception as e:
return Response({
'code': 200,
'msg': '授权成功!',
'data': {
'type': '1',
'uid': uid,
}
})
else:
return Response({
'code': 500,
'msg': '获取失败!',
})
class WeiBoBindUser(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': 500,
'msg': '参数不完整!',
})
try:
user_obj = User.objects.get(username=username)
oauth_info = OauthUser.objects.get(
uid=weibo_uid,
oauth_type=oauth_type,
user=user_obj,
)
data = {
'authenticated': True,
'id': user_obj.id,
'role': None,
'name': user_obj.nick_name,
'username': username,
'email': user_obj.email,
'token': create_token(user_obj),
'type': 0
}
return Response({
'code': 200,
'msg': "登陆成功!",
'data': data
})
except Exception as e:
password = make_password(password)
user_obj = User.objects.create(
username=username,
password=password
)
oauth_info = OauthUser.objects.create(
uid=weibo_uid,
oauth_type=oauth_type,
user=user_obj,
)
data = {
'authenticated': True,
'id': user_obj.id,
'role': None,
'name': user_obj.nick_name,
'username': username,
'email': user_obj.email,
'token': create_token(user_obj),
'type': 0
}
return Response({
'code': 200,
'msg': "登陆成功!",
'data': data
})
from oauthapp.views import *
from django.urls import path, re_path, include
urlpatterns = [
path('weibo/', WeiBoView.as_view()),
path('weibo/callback/', WeiBoCallback.as_view()),
path('weibo/binduser/', WeiBoBindUser.as_view()),
]