kmcclellan / kafka-di

An extension of Confluent's Kafka client for use with Microsoft.Extensions.DependencyInjection (and friends).
https://www.nuget.org/packages/Confluent.Kafka.DependencyInjection
MIT License
13 stars 5 forks source link

SetOAuthBearerTokenRefreshHandler #13

Open GChannonLondon opened 3 months ago

GChannonLondon commented 3 months ago

Hi,

How do I set SetOAuthBearerTokenRefreshHandler and OAuthBearerSetToken using the DI library?

var builder = serviceCollection.AddKafkaClient();

builder.Configure(
    options =>
    {
        options.OnAuthenticate()
    });
kmcclellan commented 3 months ago

That's correct you use KafkaClientOptions.OnAuthenticate(...):

https://github.com/kmcclellan/kafka-di/blob/4d10a3376808833e99bbd3ac31a7ff1925920a43/Confluent.Kafka.DependencyInjection/Options/KafkaClientOptions.cs#L198-L206

Essentially, you can pass the same delegate you would to ProducerBuilder.SetOAuthBearerTokenRefreshHandler(...) or ConsumerBuilder.SetOAuthBearerTokenRefreshHandler(...). The only difference is that I've consolidated them to a single method.

If you're wondering how to actually generate a token from the config, there's a pretty good example in the Confluent repository.