本文通过使用FastAPI启动一个获取token的接口服务,并使用postman进行获取token测试。
安装依赖
pip install pyjwt #
pip install python-multipart # OAuth2需要通过表单数据来发送信息
pyjwt生成Token细节可参考这篇文章:详解PyJWT生成Token
生成token的代码详解
from datetime import datetime, timedelta
from typing import Optional
from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
import jwt
from pydantic import BaseModel
# to get a string like this run:
# openssl rand -hex 32
SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
ALGORITHM =

本文展示了如何利用FastAPI搭建一个OAuth2授权的Token获取接口。首先介绍了所需的依赖包,然后详细解释了生成JWT Token的代码,包括设置有效期和使用pyjwt库进行编码。最后,给出了通过Postman测试该接口的说明。
&spm=1001.2101.3001.5002&articleId=109362680&d=1&t=3&u=ca3f4f310ccf456c8a7ccacc146eeced)
2599

被折叠的 条评论
为什么被折叠?



