Authentication 自动测试:在查询字符串中进行身份验证

Authentication 自动测试:在查询字符串中进行身份验证,authentication,restful-authentication,autorest,Authentication,Restful Authentication,Autorest,我使用的RESTAPI需要通过查询字符串进行身份验证 更具体地说,swagger安全性定义是 "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 whe

我使用的RESTAPI需要通过查询字符串进行身份验证

更具体地说,swagger安全性定义是

"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_令牌”:{
“类型”:“apiKey”,
“名称”:“api_令牌”,
“说明”:“身份验证通过API令牌进行处理,在向我们的API发出任何请求时,您需要将API令牌作为GET参数发送。

\n要请求API令牌,请访问我们网站上的设置页面,然后按\“将令牌发送到电子邮件\”按钮。

”, “在”:“查询” } }, “安全性”:[{“api_令牌”:[]}],
但是,单个操作不包括
api\u key
作为参数。与header不同,我不能只创建一个构造函数来设置HttpClient上的默认头

如何生成允许我设置api密钥的代码客户端


附加说明:我也接受其他发电机,如NSwag。不幸的是,从2019-06年开始,NSwag需要实现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);
        }
    }
公共类MyCreds:ServiceClientCredentials
{
公共覆盖无效初始化ServiceClient(ServiceClient客户端)
{
base.初始化ServiceClient(客户端);
}
公共覆盖任务进程HttpRequestAsync(HttpRequestMessage请求,CancellationToken CancellationToken)
{request.RequestUri=new System.Uri(request.RequestUri.OriginalString+“?api_key=abc”);
返回base.ProcessHttpRequestAsync(请求、取消令牌);
}
}

已测试并签出。不过,我不清楚是否需要更改生成配置以获得正确的构造函数。我在答案上加了这个