from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView
from urllib.parse import urlencode
# 生成前端跳转到微博扫码页面的url
class WeiboUrl(APIView):
'''
生成微博的登陆页面路由地址
https://api.weibo.com/oauth2/authorize? # 微博oauth认证地址
client_id=4152203033& # 注册开发者id
response_type=code&
redirect_uri=http://127.0.0.1:8888/oauth/callback/ # 获取code后将code回
调给后端地址
'''
# 自定义权限类
permission_classes = (AllowAny,)
def post(self, request):
url = 'https://api.weibo.com/oauth2/authorize?'
# 微博授权的 url地址
data = {
'client_id': '1625005506',
#settings.WEIBO_CLIENT_ID
'response_type': 'code',
'redirect_uri': 'http://127.0.0.1:8888/oauth/callback/',
# VUE的回调,微博后台授权的回调地址
}
weibo_url = url + urlencode(data)
# https://api.weibo.com/oauth2/authorize?
# client_id = 4152203033 & response_type = code & redirect_uri = http: // 127.0 .0
# .1: 8000 / api / weibo_back /
# return Response({'weibo_url': weibo_url})
return Response({'code': '0', 'msg': '成功', 'data': {'url': weibo_url}})