Search code examples
c#access-tokenrestsharp

The type or namespace name "IRestResponse" could not be found (are you missing a using directive or an assembly reference?)


I have a problem with IRestResponse in the below:

public async Task<CezanneToken> GetAccessToken()
{
    var client = new RestClient(WebConfigurationManager.AppSettings["TokenUrl"]);
    var request = new RestRequest();
    request.Method = Method.Post;
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("content-type", "application/x-www-form-urlencoded");
    request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=" + WebConfigurationManager.AppSettings["ClientId"] + "&client_secret=" + WebConfigurationManager.AppSettings["ClientSecret"] + "", ParameterType.RequestBody);

    IRestResponse response = await client.ExecuteAsync(request);
    string serStatus = ((RestResponseBase)response).Content;
    CezanneToken details = JsonConvert.DeserializeObject<CezanneToken>(serStatus);
    string Token = details.access_token;
    
    return details;
}

IRestResponse throws

The type or namespace name "IRestResponse" could not be found (are you missing a using directive or an assembly reference?) I cannot make it work. IntelliSense suggest to go with RestResponse> instead of IRestResponse.

But when I go with RestResponse I get Bad Request on the response.

The code sample above is "translated" from Visual Basic but it works just fine in VB. I don't know if the problem with the Bad Request comes from using RestResponse but I assume IRestResponse is needed just as in VB.

I've also seen people using IRestResponse but it just doesn't work for me. I have the using RestSharp; but do I need something else as well?


Solution

  • You will need to downgrade the RestSharp NuGet package (<=106.15).

    RestSharp got a major upgrade in v107, a Migration Guide is available.

    Depercated interfaces where removed in v107, including:

    • IRestRequest
    • IRestResponse
    • IHttp

    Documentation of where classes/interfaces were relocated can be found here