问题的解决方法是使用AutoRest生成的客户端构造函数,并在其签名中明确传递凭据信息。如果您的API需要身份验证,那么这将是必需的。在客户端类中,AutoRest默认使用以下方式生成构造函数:。
internal MyApiClient(System.Uri baseUri, System.Net.Http.HttpClient httpClient) : base(baseUri, httpClient)
{
}
要传递凭据信息,必须在签名中添加一个参数。例如:
internal MyApiClient(System.Uri baseUri, System.Net.Http.HttpClient httpClient, System.Net.ICredentials credentials) : base(baseUri, httpClient)
{
Credentials = credentials;
}
然后,您可以在创建客户端实例时传递凭据,如下所示:
var credentials = new System.Net.NetworkCredential("username", "password");
var client = new MyApiClient(new System.Uri("https://api.example.com"), new System.Net.Http.HttpClient(), credentials);
这将使您能够在使用AutoRest生成的客户端类时传递凭据信息。