我正在使用servicestack的招摇,但我从我的/资源URL获得401未经授权的错误,因为它需要API密钥 .
除非我弄错了,according to the documentation我应该从我的html页面初始化Swagger时将supportHeaderParams设置为true以及JSON参数中的apiKeyName和apiKey值 .
我当时希望在http请求标头中看到我的API密钥,但它仍然被附加到URL而不是标头集合中 .
以下是在我的HTML页面中初始化Swagger的代码:
window.swaggerUi = new SwaggerUi({
discoveryUrl: "http://pathtomyservice.com/resources",
headers: { "testheader" : "123" },
apiKey: "123",
apiKeyName: "Api-Key",
dom_id:"swagger-ui-container",
supportHeaderParams: true,
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
onComplete: function(swaggerApi, swaggerUi){
if(console) {
console.log("Loaded SwaggerUI");
console.log(swaggerApi);
console.log(swaggerUi);
}
$('pre code').each(function(i, e) {hljs.highlightBlock(e)});
},
onFailure: function(data) {
if(console) {
console.log("Unable to Load SwaggerUI");
console.log(data);
}
},
docExpansion: "none"
});
不幸的是我根本没有 Headers ,没有'Api-Key'或'testheader' .
3 回答
我认为这可能是一个招摇ui的错误 .
作为一种解决方法,我在swagger index.html文件中添加了以下内容 .
希望这可以帮助,
在swagger-ui 2.0或更高版本中,这是微不足道的:
https://github.com/wordnik/swagger-ui#header-parameters
这也更具可扩展性,并支持自定义身份验证机制 .
你可以试试这个