Using .net 6.0 and RestSharp 110.0.1 when I follow the example from the RestSharp documentation (linked below) I get an error "RestClient.Authenticator cannot be assigned to -- it is readonly."
RestSharp Documentation GitHub Gist for documentation code
public class WebInteractionsAuthenticator : AuthenticatorBase {
readonly string _baseUrl;
readonly string _clientId;
readonly string _clientSecret;
public WebInteractionsAuthenticator(string baseUrl, string clientId, string clientSecret) : base("") {
_baseUrl = baseUrl;
_clientId = clientId;
_clientSecret = clientSecret;
}
protected override async ValueTask<Parameter> GetAuthenticationParameter(string accessToken) {
Token = string.IsNullOrEmpty(Token) ? await GetToken() : Token;
return new HeaderParameter(KnownHeaders.Authorization, Token);
}
async Task<string> GetToken() {
var options = new RestClientOptions(_baseUrl);
using var client = new RestClient(options) {
[tag:// ERROR IS AT FOLLOWING LINE]
Authenticator = new HttpBasicAuthenticator(_clientId, _clientSecret),
};
var request = new RestRequest("oauth2/token")
.AddParameter("grant_type", "client_credentials");
var response = await client.PostAsync<TokenResponse>(request);
return $"{response!.TokenType} {response!.AccessToken}";
}
}
I've tried looking for alternatives and can't find anything to resolve this.
As the Obsolete said, we should use RestClientOptions.Authenticator instead, so I suggest you could try below codes:
var options = new RestClientOptions();
options.Authenticator = new HttpBasicAuthenticator("","");
using var client = new RestClient(options)
{
};