reactiveui/refit

Any complete on how to use AuthorizationHeaderValueGetter

ericontilt opened this issue · 10 comments

I am trying to get the AuthorizationHeaderValueGetter to work, but no matter what I do, I can't get the authorization header set correctly.

In my container configuration I'm setting up the API in the following way:

_services
    .AddRefitClient<IMyApi>(
        new RefitSettings
        {
            AuthorizationHeaderValueGetter = () => Task.FromResult("token")
        })
    .ConfigureHttpClient(c => c.BaseAddress = new Uri("http://my.api.com"));

My API looks like this:

public interface IMyApi
{
    [Get("/SomeEndpoint/{id}")]
    [Headers("Authorization: Basic")]
    Task<ResultObj> GetStuff(int id);
}

I first tried injecting IMyApi into one of my service classes, but the header is not extenden with the token (nor is the Func passed to the settings invoked). I also tried directly gettings the service using

RestService.For<IMyApi>(new RefitSettings { AuthorizationHeaderValueGetter = ... })

but also to no avail.

Can anyone provide a working example using the ASP.NET Core DI container with the Refit.HttpClientFactory and a dynamic Authorization header?

Same problem :(
The AuthorizationHeaderValueGetter is not called by the library, if I use the IServiceCollection.AddRefitClient.

RestService.For works fine.

Using the following will work: RestService.For<T>(string hostUrl, RefitSettings settings) while others will not

services.AddRefitClient<ISomething>() .ConfigureHttpClient(c => c.BaseAddress = new Uri("...")) .AddHttpMessageHandler<AuthorizationMessageHandler>();

For me this solution works well.

Even with 4.7.9 I can't get this to work in ASP.NET Core. The docs on this topic are just about useless. Anyone getting this to actually work?

Even with 4.7.9 I can't get this to work in ASP.NET Core. The docs on this topic are just about useless. Anyone getting this to actually work?

Thanks to the suggestions above, I did get it to work without problems. I would submit the example, but I don't have access to the code within the next two weeks.

Thanks in advance. (I still can use the solution, which I wrote until the official one)

I also can't get this to work in 4.7.9 when using in conjunction with Refit.HttpClientFactory, it only works when using the regular refit RestService.For<T>(string hostUrl, RefitSettings settings) as commented previously. As a workaround I was passing Authorization as parameter individually to each API method call, but this is a pain. Not sure about method above for calling AddHttpMessageHandler<AuthorizationMessageHandler>(); as AuthorizationMessageHandler doesn't appear to be something that is OOTB? Closest match is a handler which is internal to Refit assembly.

Well I'm glad that I'm not the only one that can't make this work as intended, as well as not being able to make sense of the docs. Rather than show a sample they show classes that one of which is internal. So for hours I'm looking at this like "do I copy and paste this class into my project?"

I will use the proposed work around, but it seems like this issue should be reopened as the commit didn't seem to actually fix this, unless we're all just using it wrong?

Edit: Hmm.. RestService.For doesn't seem to work with HttpClientFactor, unless I'm missing something there, you can't configure the client. Meanwhile the other work around using
services.AddRefitClient() .ConfigureHttpClient(c => c.BaseAddress = new Uri("...")) .AddHttpMessageHandler(); doesn't work because of the internal class. Or are you just pulling this one in manually?

Hi All,
Here is the Startup class, where I configured Refit with HttpMessageHandler.
The repository is meant to work with the Refit library (4.7.9).
For me, it works and I can use/accept this workaround.
I hope it helps you.

Ah thanks, you're just pulling that whole handler in. Which honestly is probably what I need to do anyways since I don't think I can access the current httpcontext, which is where my access token is, directly in that func anyways. Meanwhile, if anyone has any example of just integrating this with Auth0 that would be super rad ;)