标签:http author 回调 lib 扫码 res 图片 framework request
‘‘‘2.1 在apps文件夹下新建应用: oauth‘‘‘
# 首先确保cd 到apps下
cd syl/apps
python ../manage.py startapp oauth # 切换到apps文件夹下执行创建命令
# 或者输入
django-admin startapp oauth # 执行创建命令(跟上面指令二选一即可)
‘‘‘2.2 添加子路由: oauth/urls.py‘‘‘
from django.urls import path, include
from oauth.views import *
urlpatterns = [
]
‘‘‘2.3 在syl/settings.py中添加应用‘‘‘
INSTALLED_APPS = [
‘oauth‘,
]
‘‘‘2.4 在syl/urls.py主路由中添加‘‘‘
urlpatterns = [
path(‘oauth/‘, include(‘oauth.urls‘)),
]
oauth/urls.py
urlpatterns = [
path(‘weibo/‘, views.WeiboUrl.as_view()), # /oauth/weibo/ 返回微博登录地址
]
syl/settings.py
中配微博地址oauth/views.py
from rest_framework.response import Response
from rest_framework.views import APIView
from urllib.parse import urlencode
# 获取微博URL接口
class WeiBoUrl(APIView):
def post(self, request):
# 微博授权的URL地址
url = "https://api.weibo.com/oauth2/authorize?"
# AppKey:3638218081
# AppSecret:372296cdc7b6b381c1aa6f88d86f4f6e
data = {
"client_id": ‘3638218081‘, # WEIBO_APP_KEY
"response_type": ‘code‘,
‘redirect_uri‘: ‘http://127.0.0.1:8888/oauth/callback/‘ # VUE的回调,微博后台授权的回调地址
}
weibo_url = url + urlencode(data)
return Response({
‘code‘: 1000,
‘msg‘: ‘成功‘,
‘data‘: {
‘url‘: weibo_url
}
})
测试接口获取新浪微博地址
http://192.168.56.100:8888/oauth/weibo/
在浏览器访问返回地址即可回到新浪扫码界面
https://api.weibo.com/oauth2/authorize?client_id=3516473472&response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1%3A8080%2Fweibo_callback
标签:http author 回调 lib 扫码 res 图片 framework request
原文地址:https://www.cnblogs.com/chao460/p/13934731.html