API access with token

Hello,
I want to get access to the rest information

http://urltoopenhab/rest/items?recursive=false

But I get

{"error":{"message":"Authentication required","http-code":401}}

Because my system need a token for the api access.

How is the correct syntax for it to use the token authentification?

You need to provide an Authorization http header:

Authorization: Bearer <TOKEN>

how can I attache this to the url directly?

It is part of the header information. Not part of the URL.

Where / how would you like to use it ?

in Python I have an object which has

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_check_req_return', '_oauth2_token_updater', '_rules', '_validate_oauth2_config', 'create_or_update_item', 'fetch_all_items', 'get_item', 'get_item_raw', 'json_to_item', 'logger', 'logout', 'oauth2_config', 'req_get', 'req_post', 'req_put', 'rules', 'session', 'timeout', 'url_base', 'url_rest']

object.url_rest

http://localhost:8008/rest

How do I set there the header?

I want to use this script but with authentication via token

From reading the source code of that library, it doesn’t seem like authentication is supported. You’d have to make a feature request there, but the last commit is from two years ago, so keep your fingers crossed.

The underlying library python-openhab seems to support authentication and oauth tokens

As @pacive mentioned support for that is required to be added.

Hello,

here is my python code

battery_level_string = "%.9f" % battery_level
            requests.post(self.openhab.url_rest + "/items/" + config["openhab_batterylevel_item"], data=battery_level_string, headers=self.header)

But i get this error in the openhab log

No message body reader has been found for class java.lang.String, ContentType: application/octet-stream
2023-04-16 20:14:44.863 [WARN ] [s.impl.WebApplicationExceptionMapper] - javax.ws.rs.WebApplicationException: HTTP 415 Unsupported Media Type

and here the item

Number:Dimensionless Nuimo_BatteryLevel	"test [%.1f %%]"

how to solve this error?

You need to send a Content-Type: text/plain header as well (it seems it defaults to application/octet-stream)