如何在登录时保留access_token参数以备下次发布请求



在swagger中,使用api 3版本,我的登录名描述为:

/login:
post:
tags:
- user login
summary: User login
responses:
'200':
description: Successful login
content:
application/json:
schema:
$ref: '#/components/schemas/UserLogin'
'400':
description: Invalid login
operationId: postLogin
requestBody:
description: Login user fields
required: true
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
email:
type: string
default: admin@site.com
password:
type: string
default: 111111
required:
- email
- password

如果登录成功并返回了第n个数据,则我有access_token字段。

模式描述为:

components:
schemas:

UserLogin:
properties:
access_token:
type: string
user:
$ref: '#/components/schemas/UserLogin'
token_type:
type: string
user_avatar_path:
type: string
usersGroups:
type: array
expires_in:
type: integer

我需要在下一篇文章请求中使用这个access_token值来访问授权页面。

我怎么能大摇大摆地做到这一点。似乎我无法从的登录请求中复制粘贴access_token值我的帖子请求,因为你不访问_token参数?

修改块:我发现了如何从登录中复制粘贴access_token:https://prnt.sc/rs3ck6

/用于读取配置文件数据的个人/配置文件url在qiuery中没有参数,所以我将access_token定义为:

securitySchemes:
ApiKeyAuth:        # arbitrary name for the security scheme
type: apiKey
in: header       # I suppose it must be "header"
name: access_token

但是如何将access_token添加到我/个人/个人资料的标题中呢?我明白了:https://prnt.sc/rs3i90点击带有锁定符号的按钮,我看到模式对话框"可用授权",它是空的。我想我必须从中选择access_token?

谢谢!

我想到的是API密钥身份验证。这将减少您需要复制粘贴access_token的时间。

假设您需要在每个POST请求上通过查询字符串传递access_token,那么您可以像这样在全局范围内设置身份验证:

components:
securitySchemes:
ApiKeyAuth:
type: apiKey
name: access_token
in: header
security:
- ApiKeyAuth: []

或者,如果您正在使用承载身份验证:

components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
security:
- bearerAuth: []

/login调用获得access_token后,可以单击API信息下的授权按钮,并在此处进行设置。然后,通过Swagger UI进行的所有后续调用都应该在查询字符串中自动包含access_token,如下所示:

https://petstore.swagger.io/v2/login?access_token=1234

最新更新