为Swagger文档API实现OAuth2时出错

时间:2018-02-05 12:46:27

标签: rest api swagger

我有以下代码:

/oauth2/gARD:
get:
  tags:
    - RM API
  summary: Fecthes as per user's request.
  operationId: gARD
  security: 
    - OAuth2: [read]
  consumes:
    - application/json
  produces:
    - application/json
  parameters:
    - in: header
      name: token
      description: Authorization
      type: string
      required: true
  responses:
    '200':
      description: Successful 
    '500':
      description: Returns error message

我在上面代码下面的组件类别中有以下内容:

components:
  securitySchemes:
      OAuth2:
         type: oauth2
         flow:
         authorizationCode:
            authorizationUrl: https://example.com/oauth/authorize
            tokenUrl: https://example.com/oauth/token
            scopes:
               read: Grants read access
               write: Grants write access
               admin: Grants access to admin operations

this link.我引用的所有内容"安全要求必须符合安全定义"错误。

1 个答案:

答案 0 :(得分:1)

您正在混合使用OpenAPI 2.0和3.0语法。 components.securitySchemes是OpenAPI 3.0语法,但您的路径定义使用2.0语法。

假设您使用OpenAPI 2.0,安全性定义应如下所示:

securityDefinitions:
  OAuth2:
    type: oauth2
    flow: accessCode
    authorizationUrl: https://example.com/oauth/authorize
    tokenUrl: https://example.com/oauth/token
    scopes:
      read: Grants read access
      write: Grants write access
      admin: Grants access to admin operations