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		# 迁移数据库信息
    
  • 相关阅读:
    一种查找中值的方法——Rank_Select
    VS 2008 下安装OpenCV 2.3.0 .【转载】
    【转】OpenCV HOGDescriptor 参数图解 .
    VLFeat——SIFT图像特征提取(VC++实现)【转载】
    KD Tree
    【转】让任务管理器画出正弦曲线
    VLFeatmean sift开源库【配置】【转载】
    《程序员求职成功路》之字符串__strtok函数相关理解【转载】
    堆排序
    opencv imread
  • 原文地址:https://www.cnblogs.com/chao460/p/13934711.html
Copyright © 2011-2022 走看看