1. 获取微博url接口
视图函数: 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
from rest_framework_jwt.settings import api_settings
from django.contrib.auth.hashers import check_password
import requests
# 生成前端跳转到微博扫码页面的url
class OauthAccessUrl(APIView):
def get(self, request):
url = 'https://api.weibo.com/oauth2/authorize?'
data = {
'client_id': '811264755',
'redirect_uri': 'http://127.0.0.1:8888/oauth/callback'
}
# urlencode: django自带的拼接方法,不容易报错
weibo_url = url + urlencode(data)
# weibo_url结果: 'https://api.weibo.com/oauth2/authorize?client_id=811264755&redirect_uri=http://127.0.0.1:8888/oauth/callback'
return Response({
"msg": "成功", "code": 0, 'data': {
'url': weibo_url}})
3.测试生成微博售前URL接口
测试接口获取新浪微博地址
http://192.168.56.100:8888/oauth/weibo/
2. 微博回调接口
class OauthAccessToken(APIView):
"""
通过code值获取新浪微博的access_token
"""
def post(self, request):
code = request.data.get('code')
print(code)
request_data = {