我正在使用需要通过查询字符串进行身份验证的REST Api。
更确切地说,昂首阔步的安全定义是
"securityDefinitions": {
"api_token": {
"type": "apiKey",
"name": "api_token",
"description": "<p>Authentication is handled via API tokens that you need to send as a GET parameter when making any request to our API.</p>\n <p>To request an API token you need to your settings page on our website and press \"Send Token to Email\" button.</p>",
"in": "query"
}
},
"security": [{"api_token": []}],
但是,单个操作不包含api_key
作为参数。与标头不同,我不能只创建在HttpClient上设置默认标头的构造器。
如何生成允许我设置api密钥的代码客户端?
附加说明:我还将接受其他类似NSwag的生成器。不幸的是,自2019年6月起,NSwag does not handle security definitions in generation
答案 0 :(得分:1)
您需要实现ServiceClientCredentials类,并重写ProcessHttpRequestASync方法。然后自己手动添加查询字符串
public class MyCreds : ServiceClientCredentials
{
public override void InitializeServiceClient<T>(ServiceClient<T> client)
{
base.InitializeServiceClient(client);
}
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{ request.RequestUri = new System.Uri(request.RequestUri.OriginalString + "?api_key=abc");
return base.ProcessHttpRequestAsync(request, cancellationToken);
}
}