思维导图:
原理图:
Django+vue实现新浪微博登陆
新浪微博开放平台:https://open.weibo.com/
一、准备工作
-
注册新浪微博账号
-
登录微博开放平台:https://open.weibo.com
-
进入首页。创建网站接入的应用
-
选择立即接入,进入到创建应用页面
-
创建应用
-
基本信息页面
-
高级信息页面
二、接口调用
-
查看接口文档
-
OAuth2.0授权认证
接口 | 说明 |
---|---|
请求用户授权Token | |
获取授权过的Access Token, UID |
view.py:
from urllib.parse import urlencode from django.contrib.auth.hashers make_password import requests from rest_framework.views import APIView from rest_framework.response import Response from authapp.models import * from rest_framework_jwt.serializers import jwt_payload_handler, jwt_encode_handler from app01.views import jwt_response_payload_handler class WeiBoURL(APIView): def post(self, request): url = 'https://api.weibo.com/oauth2/authorize?' data = { 'client_id': '1852420066', 'redirect_uri': 'http://127.0.0.1:8888/oauth/callback', 'response_type': 'code' } weibo_url = url + urlencode(data) return Response({'code': '0', 'msg': '成功', 'data': {'url': weibo_url}}) class WeiBoCallback(APIView): def post(self, request): code = request.data.get('code') print(code) data = { 'client_id': '1852420066', 'client_secret': '447e39cc08846ca5599812c114bdd013', '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) data = weibo_data.json() weibo_uid = data.get('uid') # 获取到用户的id print(weibo_uid) if weibo_uid: try: uid_user = OauthUser.objects.get(uid=weibo_uid) user = uid_user.user payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) data = jwt_response_payload_handler(token, user) res_data = { 'code': 0, 'msg': '授权成功', 'data': { 'type': '0', 'uid': weibo_uid, 'username': uid_user.user.username, 'token': data['token'] } } return Response(res_data) except Exception as e: res_data = { 'code': 0, 'msg': '授权成功', 'data': { 'type': '1', 'uid': weibo_uid } } return Response(res_data) else: print(2) return Response({'code': 999, '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') print(username,password,weibo_uid) if not all([username,password,weibo_uid]): return Response({'code':4005,'msg':'参数不全'}) try: user = User.objects.get(username=username) OauthUser.objects.create(uid=weibo_uid,oauth_type=oauth_type,user=user) payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) data_u = jwt_response_payload_handler(token, user) data = { 'authenticated':True, 'id':user.id, 'name':user.nick_name, 'username':user.username, 'email':user.email, 'token':data_u['token'], 'type':0 } res_data = { 'code':0, 'msg':'登录成功', 'data':data } return Response(res_data) except Exception as e: password = make_password(password) user = User.objects.create(username=username,password=password) print(user,user.username) OauthUser.objects.create(uid=weibo_uid, oauth_type=oauth_type, user=user) payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) data_u = jwt_response_payload_handler(token, user) data = { 'authenticated': True, 'id': user.id, 'name': user.nick_name, 'username': username, 'email': user.email, 'token': data_u['token'], 'type': 0 } res_data = { 'code': 0, 'msg': '登录成功', 'data': data } return Response(res_data) return Response({'code': 0, 'msg': '登录成功', 'data': data})