1.获取微博授权URL接口

1.创建apps/oauth模块进行oauth认证

'''2.1 在apps文件夹下新建应用: 
oauth''' cd syl/apps python ../manage.py startapp oauth 
# 切换到apps文件夹下执行创建命令 ''
'2.2 添加子路由: oauth/urls.py'''
 from django.urls import path
  from . import views urlpatterns = [ 
  ]'''
  2.3 在syl/settings.py中添加应用''' INSTALLED_APPS = [ 'oauth.apps.OauthConfig', ]''
  '2.4 在syl/urls.py主路由中添加''' urlpatterns = [ path('oauth/', include('oauth.urls')), ]

2.生成微博授权URL接口

1.1 添加子路由: oauth/urls.py

urlpatterns = [ 
path('weibo/', views.WeiboUrl.as_view()), 
# /oauth/weibo/ 返回微博登录地址 ]

1.2 视图函数: oauth/views.py

from rest_framework.permissions import AllowAny 
from rest_framework.response import Response 
from rest_framework.views import APIView from urllib.parse import urlencode

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': '3301838823',  # WEIBO_APP_KEY,
                '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/we ibo_back/
            # return Response({'weibo_url': weibo_url})
            return Response({'code': '0', 'msg': '成功', 'data': {'url': weibo_url}})

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值