美文网首页
django + oauth 开放平台

django + oauth 开放平台

作者: Mead170810 | 来源:发表于2019-12-03 10:54 被阅读0次

    项目使用django2.1 + oauth server 搭建开放平台代码。
    开放给外部用户申请 appid, appkey,
    源码地址

    项目说明:

    使用 (oauth2 server django)django2.1 + oauth server,搭建oauth server端,构建基础 开放平台。

    项目亮点:

    1. 使用django2.1+python3.5 部署开放平台基础代码;
    2. 使用 anaconda 搭建环境,方便部署在不同服务器上;
    3. 与baidu音箱api对接完成,方便扩展使用;

    项目部署说明

    • 使用anaconda3 download link 部署的开发环境 conda 4.7.12, pip 9.0.1
    • 项目使用 django 2.1.1, python3.5
    • 环境配置 environment.yaml
    • 扩展 requirements.txt
    • nginx+uwsgi 配置文件: nginx_oauth.conf
    • 演示站点

    环境安装说明:

    - install anaconda # 安装 anaconda
    - conda create -n oauth python=3.5
    - conda activate oauth # 激活虚拟空间
    - conda env update -f environment.yaml # 导入环境
    - pip install --upgrade pip # 升级 pip
    - pip install -r requirements.txt # 导入需要的扩展
    - python manage.py runserver 0.0.0.0:8003 # 测试
    - python manage.py collectstatic # 
    - uwsgi --ini uwsgi_oauth.ini # 启动 uwsgi 服务
    - 配置nginx (配置文件位置: nginx_oauth.conf)
    

    nginx配置参考文档:

    https://www.centos.bz/2017/08/nginx-virtualenv-uwsgi-django/
    

    使用说明:

    • 客户端
    Authorize url: https://open.yourhost.com/o/authorize/
    Token url: https://open.yourhost.com/o/token/
    
    - 拼装URL https://open.yourhost.com/o/authorize?client_id=<your_client_id>&response_type=code&state=random_state_string
    - 打开URL,登录并确认授权
    - 页面会回调到 redirect url
    - 通过 code 得到 access_token
    - 通过 access token 请求数据
    curl -X POST -H "Authorization: Bearer GruMdae4Oo7udKiWdVZOqr7O6BiiIF" https://open.yourhost.com/dueros/get_video_url
    

    备注:

    • 导出与导入 requirements.txt environment.yaml
    conda env export > environment.yaml # 客户端(导出)
    conda env create -f environment.yaml # 服务器(导入环境)
    
    pip freeze > requirements.txt # 客户端(导出)
    pip install -r requirements.txt # 服务器(导入)
    
    ## nginx 服务常用命令
    systemctl start nginx
    systemctl stop nginx
    systemctl reload nginx
    
    ## uwsgi 常用命令
    uwsgi --ini uwsgi_oauth.ini
    uwsgi --reload uwsgi_oauth.pid
    

    参考信息

    oauth Server端验证

    oauth 配置一说明
    oauth 配置二说明

    - Get your token and use your API
    curl -X POST -d "grant_type=password&username=<user_name>&password=<password>" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/
    curl -X POST -H "Authorization: Bearer l8UxYs6x3ETvq7pSTxKlK0ZDpRzQn4" -d "grant_type=password&username=<user_name>" http://localhost:8000/dueros/get_video_url
    

    安装好oauth服务端,默认会有 o/applications/可以管理,现在我将applications各页面重写了。
    重写的方法在management/views.py

    管理员账号密码:
    admin/admin

    部分截图页面:

    首页
    应用列表有记录 授权页
    确认授权

    更多截图

    conda 添加源

    #查看环境变量:
    (base) vip39@VM-0-15-ubuntu:~/src$ vim ~/.condarc
    # 目前有的channels:
    channels:
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
      - conda-forge
      - defaults
    show_channel_urls: true
    # bioconda需要放在第一位,优先搜索软件;forge第二位
    

    相关文章

      网友评论

          本文标题:django + oauth 开放平台

          本文链接:https://www.haomeiwen.com/subject/ghccgctx.html