Swagger编辑器未在请求标头中传递授权

时间:2017-04-13 13:58:47

标签: cors swagger swagger-editor

我第一次使用Swagger Editor来测试API。我在本地运行它。我的授权正在运行,但是我的第一个GET路径模式一直在返回"无法获取"错误。

当我在GitBash中运行Swagger生成的Curl代码时,我得到了我期望的结果,所以我需要的只是在架构中。我在Swagger编辑器中运行它时,我无法得到响应。

据我所知,Swagger Editor未在请求标头中包含授权令牌。这是我的浏览器控制台的请求... enter image description here

我知道这可能是一个CORS问题 - 但我不是服务器管理员,也没有权限对服务器进行更改。我需要告诉服务器管理员吗?或者我需要在本地计算机上更改某些内容以包含带有标头请求的授权代码?我的服务器管理员说CORS已正确启用。

奇怪的是,如果我复制由Swagger编辑器生成的CURL字符串,并将其粘贴到GitBash中,它会正常运行并得到我期望的结果。这是CURL sting ......

enter image description here

如果有帮助,这是我的Swagger JSON。

{  
   "swagger":"2.0",
   "info":{  
      "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi molestie sem nec nibh blandit efficitur. Donec arcu massa, semper ut mauris eu, fermentum dictum turpis. [Test Link](http://google.com) Duis efficitur at sapien non maximus. ",
      "version":"1.0.0",
      "title":"LA-API",
      "termsOfService":"http://swagger.io/terms/",
      "contact":{  
         "email":"apiteam@swagger.io"
      },
      "license":{  
         "name":"Apache 2.0",
         "url":"http://www.apache.org/licenses/LICENSE-2.0.html"
      }
   },
   "host":"betaapi.myDomain.com",
   "basePath":"/",
   "tags":[  
      {  
         "name":"Account",
         "description":"First try at adding LA-API Swagger paths",
         "externalDocs":{  
            "description":"Find out more",
            "url":"http://swagger.io"
         }
      }
   ],
   "schemes":[  
      "http"
   ],
   "securityDefinitions":{
      "la-api_auth":{  
         "type":"oauth2",
         "tokenUrl":"http://betaauthorize.myDomain.com/access_token",
         "flow":"password",
         "scopes":{  
            "write:la":"Lorem ipsum",
            "read:la":"Dolor sit amet"
         }
      },
      "api_key":{  
         "type":"apiKey",
         "name":"Authorization",
         "in":"header"
      }
   },
   "paths":{
      "/api/account/{account_id}":{  
         "get":{  
            "tags":[  
               "Account"
            ],
            "summary":"Account - View",
            "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "operationId":"AccountView",
            "produces":[  
               "application/json"
            ],
            "parameters":[
               {  
                  "name":"account_id",
                  "in":"path",
                  "description":"ID of Account to return",
                  "required":true,
                  "type":"integer",
                  "format":"int64"
               }
            ],
            "security":[  
               {  
                  "la-api_auth":[  
                     "write:la",
                     "read:la"
                  ]
               }
            ],
            "responses":{  
               "200":{  
                  "description":"successful operation"
               },
               "400":{  
                  "description":"Invalid Account ID supplied"
               },
               "401":{
                  "description":"Authorization token is required and has failed or has not yet been provided"
               },
               "404":{  
                  "description":"Account ID not found"
               }
            }
         }
      }
   },
   "definitions":{  
      "Account":{
         "type":"object",
         "properties":{
            "sms_id":{  
               "type":"integer",
               "format":"int64",
               "example":"370"
            },
            "address1":{  
               "type":"string",
               "example":"123%"
            },
            "city":{  
               "type":"string",
               "example":"Test%"
            },
            "state_id":{  
               "type":"string",
               "example":"TN"
            }
         }
      },
      "ApiResponse":{  
         "type":"object",
         "properties":{  
            "code":{  
               "type":"integer",
               "format":"int32"
            },
            "type":{  
               "type":"string"
            },
            "message":{  
               "type":"string"
            }
         }
      }
   },
   "externalDocs":{  
      "description":"Find out more about Swagger",
      "url":"http://swagger.io"
   }
}

任何建议都将不胜感激。感谢。

1 个答案:

答案 0 :(得分:2)

使用此资源:https://www.w3.org/TR/cors/#cross-origin-request-with-preflight-0

OPTIONS请求应排除用户凭据,因此服务器也不应使用401响应。