django + oauth 开放平台

2019-12-03  本文已影响0人  Mead170810

项目使用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对接完成,方便扩展使用;

项目部署说明

环境安装说明:

- 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

备注:

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第二位
上一篇下一篇

猜你喜欢

热点阅读