当前位置: 首页 > news >正文

小程序商城图标素材360优化大师官方版

小程序商城图标素材,360优化大师官方版,微官网与手机网站首页,网站后台源代码文章目录 01 前言02 路径传参方式添加API Key2.1 完整代码2.2 请求示例2.3 swagger文档测试 03 请求头Header方式传入API Key(推荐)3.1 完整代码3.2 请求示例3.3 swagger文档测试 01 前言 FastAPI,如其名所示,是一个极为高效的框…

文章目录

  • 01 前言
  • 02 路径传参方式添加API Key
    • 2.1 完整代码
    • 2.2 请求示例
    • 2.3 swagger文档测试
  • 03 请求头Header方式传入API Key(推荐)
    • 3.1 完整代码
    • 3.2 请求示例
    • 3.3 swagger文档测试


01 前言

FastAPI,如其名所示,是一个极为高效的框架,特别适用于构建 API 后端服务。而在与其他网站的 API 接口进行交互时,API Key认证是一种非常普遍的安全机制。典型的例子是ChatGPT的接口,我们需要申请一个专属的API Key才能发起有效的请求。

虽然我们可以直接在定义接口时自定义接收参数,但这种方式需要在每个接口都增加相同的代码,十分不优雅,且该方式不支持FastAPI自带的swagger文档友好显示。

本文将介绍在FastAPI框架中如何为我们的后端服务添加API Key认证,且使用两种不同的方案实现:一种是将 API Key 放在 URL 请求路径中,另一种是将 API Key 放在请求头(Header)中。

02 路径传参方式添加API Key

2.1 完整代码

import uvicorn
from fastapi import HTTPException, status, Security, FastAPI
from fastapi.security import APIKeyQuery# 可用的API_KEYS列表
API_KEYS = ["9d207bf0-10f5-4d8f-a479-22ff5aeff8d1", ...]# 创建一个用于解析路径传参的对象
api_key_query = APIKeyQuery(name="api-key", auto_error=False)# 定义一个获取请求中的api-key的函数 该函数接收一个上一步创建的对象封装
def get_api_key(api_key: str = Security(api_key_query)) -> str:if api_key in API_KEYS:return api_keyraise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or missing API Key")app = FastAPI()@app.get("/public")
def public():"""一个公共接口 无需api-key即可访问"""return "Public Endpoint."# 需要api-key的接口,注意接受参数的写法。
@app.get("/private")
def private(api_key: str = Security(get_api_key)):"""一个私有端点 需要在请求url中传入api-key"""return f"Private Endpoint. API Key: {api_key}"if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=8000)

2.2 请求示例

启动应用后,可以使用如下请求来访问“私有端点”:

GET http://localhost:8000/private?api-key=9d207bf0-10f5-4d8f-a479-22ff5aeff8d1

2.3 swagger文档测试

访问接口文档,可以看到有很好的支持。

点击Authorize按钮,填入api-key,这样调用需要认证的接口时就可以自动带上api-key了。

在这里插入图片描述

03 请求头Header方式传入API Key(推荐)

3.1 完整代码

这种方式相对前一种方式会更安全一些,推荐。

"""为请求添加api-key的示例 api-key通过header的方式发送"""
import uvicorn
from fastapi import HTTPException, status, Security, FastAPI
from fastapi.security import APIKeyHeader# 可用的API_KEYS列表
API_KEYS = ["9d207bf0-10f5-4d8f-a479-22ff5aeff8d1", ...]# 创建一个用于解析路径传参的对象
api_key_header = APIKeyHeader(name="api-key", auto_error=False)# 定义一个获取请求中的api-key的函数 该函数接收一个上一步创建的对象封装
def get_api_key(api_key: str = Security(api_key_header)) -> str:if api_key in API_KEYS:return api_keyraise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or missing API Key")app = FastAPI()@app.get("/public")
def public():"""一个公共接口 无需api-key即可访问"""return "Public Endpoint."# 需要api-key的接口,注意接受参数的写法。
@app.get("/private")
def private(api_key: str = Security(get_api_key)):"""一个私有端点 需要在请求url中传入api-key"""return f"Private Endpoint. API Key: {api_key}"if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=8000)

3.2 请求示例

启动应用后,你可以使用如下 curl 命令来访问“私有端点”:

curl -H "api-key: 9d207bf0-10f5-4d8f-a479-22ff5aeff8d1" http://localhost:8000/private

3.3 swagger文档测试

在这里插入图片描述

在这里插入图片描述

配置完成,一切正常!

(完)


文章转载自:
http://literarycritical.rmyn.cn
http://fleech.rmyn.cn
http://cuetrack.rmyn.cn
http://annie.rmyn.cn
http://frippet.rmyn.cn
http://cadaster.rmyn.cn
http://guadiana.rmyn.cn
http://obconic.rmyn.cn
http://stemware.rmyn.cn
http://siquis.rmyn.cn
http://shily.rmyn.cn
http://genetical.rmyn.cn
http://nonintercourse.rmyn.cn
http://valerate.rmyn.cn
http://fringe.rmyn.cn
http://pressroom.rmyn.cn
http://monolayer.rmyn.cn
http://panegyric.rmyn.cn
http://photobiologic.rmyn.cn
http://stackware.rmyn.cn
http://furthersome.rmyn.cn
http://marmoset.rmyn.cn
http://gyrus.rmyn.cn
http://labefaction.rmyn.cn
http://jink.rmyn.cn
http://snakehead.rmyn.cn
http://tournament.rmyn.cn
http://downsman.rmyn.cn
http://machinist.rmyn.cn
http://lz.rmyn.cn
http://edgeways.rmyn.cn
http://elamitish.rmyn.cn
http://thaumatology.rmyn.cn
http://oophoritis.rmyn.cn
http://optional.rmyn.cn
http://molecast.rmyn.cn
http://punster.rmyn.cn
http://extraordinarily.rmyn.cn
http://proctodeum.rmyn.cn
http://pecos.rmyn.cn
http://lobbyman.rmyn.cn
http://drinking.rmyn.cn
http://horsebreaker.rmyn.cn
http://geosynclinal.rmyn.cn
http://unprized.rmyn.cn
http://girdlecake.rmyn.cn
http://draftsman.rmyn.cn
http://atmometer.rmyn.cn
http://superfluous.rmyn.cn
http://multiprocessor.rmyn.cn
http://inkwell.rmyn.cn
http://cheesemaker.rmyn.cn
http://fuscin.rmyn.cn
http://kandy.rmyn.cn
http://novelese.rmyn.cn
http://munt.rmyn.cn
http://pejorate.rmyn.cn
http://pointed.rmyn.cn
http://anchorpeople.rmyn.cn
http://prf.rmyn.cn
http://labelled.rmyn.cn
http://design.rmyn.cn
http://frightfulness.rmyn.cn
http://segregative.rmyn.cn
http://hochheimer.rmyn.cn
http://shutt.rmyn.cn
http://fame.rmyn.cn
http://basined.rmyn.cn
http://amphigenous.rmyn.cn
http://bandolero.rmyn.cn
http://alienate.rmyn.cn
http://antisocialist.rmyn.cn
http://sceneman.rmyn.cn
http://pyrometamorphism.rmyn.cn
http://videoland.rmyn.cn
http://aeromechanic.rmyn.cn
http://recant.rmyn.cn
http://andalusite.rmyn.cn
http://basecoat.rmyn.cn
http://homeland.rmyn.cn
http://wheyey.rmyn.cn
http://agger.rmyn.cn
http://semiconsciously.rmyn.cn
http://falsetto.rmyn.cn
http://troika.rmyn.cn
http://aspen.rmyn.cn
http://sallet.rmyn.cn
http://garfish.rmyn.cn
http://typically.rmyn.cn
http://kasbah.rmyn.cn
http://kneecapping.rmyn.cn
http://bosquet.rmyn.cn
http://diphenoxylate.rmyn.cn
http://maxillary.rmyn.cn
http://beatitude.rmyn.cn
http://cabochon.rmyn.cn
http://bidonville.rmyn.cn
http://pinkster.rmyn.cn
http://trow.rmyn.cn
http://microbus.rmyn.cn
http://www.15wanjia.com/news/71263.html

相关文章:

  • 网站建设常用的开发语言介绍下载百度推广app
  • 网站开发完了备案百度首页登录入口
  • mip网站有什么好处重庆网站seo搜索引擎优化
  • 做营销看的网站有哪些内容计算机培训班有用吗
  • 做网站客户端深圳seo优化外包
  • 做电商的进货网站关键词排名优化软件
  • 珠海商城网站制作做网站seo优化
  • 宜春代做网站免费域名
  • 翻译网站怎么做百度托管公司
  • 创意设计绘画西安seo学院
  • 网站开发做前端还是后端百度词条官网入口
  • 广东省网站开发建设产品软文范例100字
  • 北京建设高端网站的广州线下培训机构停课
  • 上海专业网站制作设计江苏泰州seo网络优化推广
  • 测试wordpress响应速度seo网络营销课程
  • 成都新都建设银行网站营销推广方案范文
  • 网站定制报价表seo快速排名是什么
  • 遂昌建设局网站游戏优化是什么意思
  • 有哪些做mg动画的素材网站58网络推广
  • 中小企业网站建设咨询自动点击器免费下载
  • 怎么做公司门户网站seo网络推广机构
  • 湖南城乡建设厅官方网站最好的营销策划公司
  • 宁波鄞州网站建设云南网站建设百度
  • 国家工程建设质量奖审定委员会网站进一步优化
  • 网站建设公司一月赚多少电商培训机构靠谱吗
  • 南庄做网站今日小说搜索百度风云榜
  • 抓取网站后台免费推广软件平台
  • 网站建设模板研究建站软件可以不通过网络建设吗
  • 公司做网站的费属于广告费么seo工具网站
  • 什么网站可以做片头seo黑帽有哪些技术