zoukankan      html  css  js  c++  java
  • 微博回调接口

    1.微博回调接口

    1.1 oauth/urls.py 中添加路由

    urlpatterns = [
        path('weibo/callback/', views.OauthWeiboCallback.as_view()), # /oauth/weibo/callback/ ]
    

    1.2 oauth/views.py 中添加试图函数

    http://192.168.56.100:8888/oauth/weibo/callback/
    
    import requests
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from oauth.models import *
    
    
    # 微博回调接口
    class WeiBoCallback(APIView):
    
        def post(self, request):
    
            code = request.data.get("code")
            # print(code)
    
            url = "https://api.weibo.com/oauth2/access_token"
    
            data = {
                # WEIBO_APP_KEY
                "client_id": '3638218081',
                # WEIBO_APP_SECRET
                "client_secret": '372296cdc7b6b381c1aa6f88d86f4f6e',
                'grant_type': 'authorization_code',
                'code': code,
                'redirect_uri': 'http://127.0.0.1:8888/oauth/callback/',
            }
    
            # 需要一个http 请求去请求微博准备的信息 --- requests
    
            weibo_data = requests.post(url=url, data=data)
            # print(type(weibo_data))
            json_weibo_data = weibo_data.json()
            uid = json_weibo_data.get("uid")
    
            if uid:
                try:
                    uid_user = OauthUser.objects.get(uid=uid)
                    res_data = {
                        "code": 1000,
                        "msg": "授权成功",
                        "data":{
                            "type": "0",
                            "uid": uid,
                            "username": uid_user.user.username,
                            "token": create_token(uid_user.user)
                        }
                    }
    
                    return Response(res_data)
                except Exception as e:
    
                    res_data = {
                        "code": 1000,
                        "msg": "授权成功",
                        "data": {
                            "type": "1",
                            "uid": uid,
                        }
                    }
    
                    return Response(res_data)
            else:
                return Response({
                    "code": 999,
                    "msg": "获取微博信息失败"
    
                })
    
    

    1.3 oauth/models.py 中添加用户绑定模型

    # 把三方的用户信息,和本地的用户信息进行绑定 
    
    from django.db import models
    from user.models import User
    
    
    class OauthUser(models.Model):
    
        OAUTHTYPE = (
            ('1', 'weibo'),
            ('2', 'weixin'),
        )
        # 三方用户id
        uid = models.CharField('三方用户id', max_length=64)
        # 本地用户外键 关联User表
        user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    
        oauth_type = models.CharField('认证类型', max_length=10, choices=OAUTHTYPE)
    
        class Meta:
            db_table = "tb_oauthuser"
    

    1.4 迁移数据库

    python manager.py makemigrations 	# 生成迁移文件
    python manager.py migrate		# 迁移数据库信息
    
  • 相关阅读:
    61. 最长不含重复字符的子字符串
    60. 礼物的最大价值 (未理解)
    59. 把数字翻译成字符串
    58. 把数组排成最小的数
    57. 数字序列中某一位的数字 (不懂)
    spring data jpa 官方文档
    idea 编译报错 源发行版 1.8 需要目标发行版 1.8
    idea maven 依赖报错 invalid classes root
    solr
    spring boot 官方文档
  • 原文地址:https://www.cnblogs.com/chao460/p/13934711.html
Copyright © 2011-2022 走看看