Not getting org.openhab.core.auth.client.oauth2.OAuthFactory injected by activation

Hi,

I am playing with writing a new binding for a web service which uses OAuth 2.

It seems to me that org.openhab.core.auth.client.oauth2.OAuthFactory is the preferred way to add OAuth 2 client authentication in OpenHAB(?), so I try to use netatmo as a blueprint to see if I can get it to work.

But I am not able to get the OAuthFectory injected with the activation. HttpClientFactory and HttpService works. If I remove OAuthFactory from the constructor parameters, my HandlerFactory is initialized and discovered.

import org.openhab.core.auth.client.oauth2.OAuthFactory;

@Activate
public WithingsHandlerFactory(final @Reference HttpClientFactory factory, final @Reference HttpService httpService,
        final @Reference OAuthFactory oAuthFactory) {

The error messages I get says that it is looking for org.openhab.core.auth.client.oauth2.OAuthFactory, but can’t find it to satisfy the dependency:

16:46:14.205 [DEBUG] [hings.internal.WithingsHandlerFactory] - bundle org.openhab.binding.withings:4.2.0.202312311545 (258)[org.openhab.binding.withings.internal.WithingsHandlerFactory(330)] : New service tracker for $002, initial active: false, previous references: {}, classFilter: (objectClass=org.openhab.core.auth.client.oauth2.OAuthFactory), initialReferenceFilter (object Class=org.openhab.core.auth.client.oauth2.OAuthFactory)
16:46:14.206 [DEBUG] [hings.internal.WithingsHandlerFactory] - bundle org.openhab.binding.withings:4.2.0.202312311545 (258)[org.openhab.binding.withings.internal.WithingsHandlerFactory(330)] : Dependency not satisfied: $002

The generated MANIFEST.MF contains the reference:

osgi.service;filter:="(objectClass=org.openhab.core.auth.client.oauth2.OAuthFactory)";effective:=active,

I am working on 4.2-SNAPSHOT.

Is oauth2.OAuthFactory the prefered way to implement OAuth 2 client authentication in OpenHAB?
If so: Any tips on how I debug/get the factory injected as part of activation?

Make sure oauth2 feature is installed or declared as dependency of your binding within feature. It might be that oauth client is not installed by default as it is essential to addons, but not core.

Hi @splatch! Thanks for the quick reply! Could you please elaborate a little bit more so I understand it fully?

How do I e.g. declare a dependency within feature? My feature/feature.xml file says nothing about oauth2, but neither does the one for org.openhab.binding.automower, nor org.openhab.binding.netatmo - which both seems to work with the injected OAuthFactory at activation time. The pom.xml files also doesn’t mention it. Is there some other place I should declare this?

Fun fact: installing the automower binding made my new binding also inintialize, so they have found some way to make OSGi understand what to load.

<feature name="openhab-binding-xyz">
  <feature>openhab-core-auth-oauth2client</feature>
  …
</feature>

This is the way to refer non essential parts of openhab-core within addon you develop.
There is automatic generation of feature file, but it works at maven dependency level.

1 Like

If you use the skeleton scripts to create a binding the feature.xml contains:

	<feature name="openhab-binding-${bindingId}" description="${bindingIdCamelCase} Binding" version="${project.version}">
		<feature>openhab-runtime-base</feature>
		<bundle start-level="80">mvn:org.openhab.addons.bundles/${rootArtifactId}/${project.version}</bundle>
	</feature>

You’ll then get all features installed by openhab-runtime-base which will install openhab-transport-http which installs openhab-core-auth-oauth2client .

If you have this issue when debugging in Eclipse make sure to add the org.openhab.core.auth.oauth2client bundle to app.bndrun.

1 Like

I have the dependency in the feature.xml file, same as the other bindings that has it.

I’m starting to think that my problem is because I am developing and just copying the jar file over to the addons folder to try it out my changes, thus not getting the keraf dependencies?

I can reproduce it and think it’s a bug that got introduced during the Karaf 4.4.4 upgrade (#3814).

The oauth2client feature used to be defined as:

<feature>openhab-core-auth-oauth2client</feature>

and was changed into:

<feature dependency="true">openhab-core-auth-oauth2client</feature>

Looks like it doesn’t install feature dependencies so we probably should change it back.

Meanwhile you can get it installed on the Console using:

feature:install openhab-core-auth-oauth2client

This PR will fix it:

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.