我试图让我的Web API项目使用Swagger“漂亮”的文档等(http://swagger.io/)。
我正在使用从NuGet安装的.NET的Swashbuckle,我使用的版本是4.0.1
我已经能够安装和使用Swagger。到目前为止,一切都正常。我唯一的障碍是禁用API密钥并具有使用OAuth的能力,就像在PetStore示例中一样(http://petstore.swagger.wordnik.com/#!/pet/addPet)
我尝试了我能在网上找到的一切。以下是它们的列表:
首先,这是我的Startup.cs
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
WebApiConfig.Register(config);
Swashbuckle.Bootstrapper.Init(config);
}
现在,我的SwaggerConfig.cs文件:
public static void Register()
{
Swashbuckle.Bootstrapper.Init(GlobalConfiguration.Configuration);
SwaggerSpecConfig.Customize(c =>
{
c.IgnoreObsoleteActions();
c.IncludeXmlComments(GetXmlCommentsPath());
c.ApiInfo(new Info
{
Title = "Work you",
Description = "testing some stuffs",
Contact = "Email@email.com"
});
c.Authorization("oauth2", new Authorization
{
Type = "oauth2",
Scopes = new List<Scope>
{
new Scope { ScopeId = "products.read", Description = "View products" },
new Scope { ScopeId = "products.manage", Description = "Manage products" }
},
GrantTypes = new GrantTypes
{
ImplicitGrant = new ImplicitGrant
{
LoginEndpoint = new LoginEndpoint
{
Url = "https://www.mysecure.website.com"
},
TokenName = "access_token"
}
}
});
});
SwaggerUiConfig.Customize(c =>
{
c.EnableOAuth2Support("client_id", "test-realm", "app Name");
var thisAssembly = typeof(SwaggerConfig).Assembly;
c.SupportHeaderParams = true;
c.DocExpansion = DocExpansion.List;
c.SupportedSubmitMethods = new[] { HttpMethod.Get, HttpMethod.Post, HttpMethod.Put, HttpMethod.Head };
c.EnableDiscoveryUrlSelector();
});
}
我有一个SwaggerExtensions文件夹,在其中我有应该所需的文件。例如:
我有用装饰符(decorator)修饰的类:
[ScopeAuthorize("this.scope")]
然而,在我的Swagger页面上,OAuth选项从未显示。我也看不到我应该输入自定义标头的位置。
我可以看到标题和文档描述、电子邮件地址等是从SwaggerConfig.cs中读取的,所以我知道它至少已经被读取。
我无法弄清楚问题所在。 :(
有什么想法吗?
c.OAuth2("oauth2") ..... .Scopes(scopes => { scopes.Add("managerapi", "DESCRIPTION"); });
“oauth2”字符串必须与我的原始响应中显示的字符串匹配。 - Erik Dahl