# Sample of token based authorization

**URL:** <https://community.openhab.org/t/sample-of-token-based-authorization/67021>\
**Category:** Add-ons\
**Created:** [February 8, 2019, 8:53am UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021 "2019-02-08T08:53:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tnemrap](https://community.openhab.org/user_avatar/community.openhab.org/tnemrap/32/61523_2.png) [@tnemrap](https://community.openhab.org/u/tnemrap)\
**Post date:** [February 8, 2019, 8:53am UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021/1 "2019-02-08T08:53:47Z")

</div>

I am planning to create a binding for Husqvarna AutoMover as my second binding.  
I will look at [https://github.com/chrisz/pyhusmow](https://github.com/chrisz/pyhusmow)  
It seems that it uses Token based authorization.  
Is there any other binding that uses this where i can look.  
I am still struggling with learning Java 🙂

/Mike

---

<div class="post-metadata">

**Author:** ![hilbrand](https://community.openhab.org/user_avatar/community.openhab.org/hilbrand/32/72354_2.png) [@hilbrand](https://community.openhab.org/u/hilbrand)\
**Post date:** [February 8, 2019, 9:11am UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021/2 "2019-02-08T09:11:05Z")

</div>

> [@tnemrap](#):
>
> Token based authorization

Do you know which protocol? OAuth2? or something custom?

---

<div class="post-metadata">

**Author:** ![tnemrap](https://community.openhab.org/user_avatar/community.openhab.org/tnemrap/32/61523_2.png) [@tnemrap](https://community.openhab.org/u/tnemrap)\
**Post date:** [February 8, 2019, 9:24am UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021/3 "2019-02-08T09:24:09Z")

</div>

Seems in pyton that you post username and password and receive token informattion.

```csv
    def login(self, login, password):
        response = self.session.post(self._API_IM + 'token',
                                     headers=self._HEADERS,
                                     json={
                                         "data": {
                                             "attributes": {
                                                 "password": password,
                                                 "username": login
                                             },
                                             "type": "token"
                                         }
                                     })

        response.raise_for_status()
        self.logger.info('Logged in successfully')

        json = response.json()
        self.set_token(json["data"]["id"], json["data"]["attributes"]["provider"])
        return json["data"]["attributes"]["expires_in"]

```

/mike

---

<div class="post-metadata">

**Author:** ![hilbrand](https://community.openhab.org/user_avatar/community.openhab.org/hilbrand/32/72354_2.png) [@hilbrand](https://community.openhab.org/u/hilbrand)\
**Post date:** [February 8, 2019, 9:42am UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021/4 "2019-02-08T09:42:40Z")

</div>

I don’t know what protocol that is. It looks custom and simple. There is probably no standard support out-of-the-box for it. So you need to implement something yourself. Using `HttpUtil` you should be able to get the token and store it in some cache. For some inspiration look this earlier Spotify api implementation. It’s based on OAuth2. The earlier implementation had OAuth2 implemented, but current PR uses the OAuth2 implementation part of openHAB. To give you an example here is the token cache handling:  
[https://github.com/Hilbrand/openhab2-addons/blob/spotify\_2.4.0/addons/binding/org.openhab.binding.spotify/src/main/java/org/openhab/binding/spotify/internal/oauth2/SpotifyAccessTokenCache.java](https://github.com/Hilbrand/openhab2-addons/blob/spotify_2.4.0/addons/binding/org.openhab.binding.spotify/src/main/java/org/openhab/binding/spotify/internal/oauth2/SpotifyAccessTokenCache.java)

Just browser there to other files too and copy what you can use. Hope this helps somewhat.

---

<div class="post-metadata">

**Author:** ![tnemrap](https://community.openhab.org/user_avatar/community.openhab.org/tnemrap/32/61523_2.png) [@tnemrap](https://community.openhab.org/u/tnemrap)\
**Post date:** [February 8, 2019, 3:48pm UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021/5 "2019-02-08T15:48:50Z")

</div>

Thanks now i have somewhere to start.

/Mike

---

<div class="post-metadata">

**Author:** ![Rado1](https://community.openhab.org/user_avatar/community.openhab.org/rado1/32/32585_2.png) [@Rado1](https://community.openhab.org/u/Rado1)\
**Post date:** [April 20, 2019, 5:51pm UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021/6 "2019-04-20T17:51:40Z")

</div>

Hello,

Do you have a test version already?

I will be happy to test it?

---

<div class="post-metadata">

**Author:** ![tnemrap](https://community.openhab.org/user_avatar/community.openhab.org/tnemrap/32/61523_2.png) [@tnemrap](https://community.openhab.org/u/tnemrap)\
**Post date:** [April 21, 2019, 3:36pm UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021/7 "2019-04-21T15:36:35Z")

</div>

Not yet still struggling with token based login in java.

/mike

---

<div class="post-metadata">

**Author:** ![Marfi](https://community.openhab.org/user_avatar/community.openhab.org/marfi/32/119518_2.png) [@Marfi](https://community.openhab.org/u/Marfi)\
**Post date:** [April 5, 2020, 8:45pm UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021/8 "2020-04-05T20:45:57Z")

</div>

Hi Michael,

Do you know that there is an open API for Husqvarna Automower?

> **[Husqvarna Developer Portal](https://developer.husqvarnagroup.cloud/apis/Automower+Connect+API)**
>
> Husqvarna Group - Developer Portal

Regards  
Martin

---

<div class="post-metadata">

**Author:** ![tnemrap](https://community.openhab.org/user_avatar/community.openhab.org/tnemrap/32/61523_2.png) [@tnemrap](https://community.openhab.org/u/tnemrap)\
**Post date:** [April 6, 2020, 10:56am UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021/9 "2020-04-06T10:56:07Z")

</div>

I Know but was hoping this should happen 🙂

> [@Automower Binding (G2)](https://community.openhab.org/t/automower-binding-g2/2854/25):
>
> I not really started, except setting up the development environment (which is up-and-running) My spare time is limited atm. But I hope to free more time from mid december. Anyway, my mower currently hangs around as the season is over wink I will post a new thread as soon something is ready to test.

/Mike

---

<div class="post-metadata">

**Author:** ![HaKuNa](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/c77e96/32.png) [@HaKuNa](https://community.openhab.org/u/HaKuNa)\
**Post date:** [April 6, 2020, 11:08am UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021/10 "2020-04-06T11:08:00Z")

</div>

I started but with slow progress. Then I realized the API does not provide position coordianes (which is possible via the husmov solution). Means, atm I do not continue with the binding. But I hope the API will be expanded and will provide more data in the future. Until then I decided to live with the current solution.

 ![mower](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/5/1/513610f221b9ca9b94ab8cdd0be631735de0e016.jpeg)

---

<div class="post-metadata">

**Author:** ![tnemrap](https://community.openhab.org/user_avatar/community.openhab.org/tnemrap/32/61523_2.png) [@tnemrap](https://community.openhab.org/u/tnemrap)\
**Post date:** [April 6, 2020, 2:27pm UTC](https://community.openhab.org/t/sample-of-token-based-authorization/67021/11 "2020-04-06T14:27:30Z")

</div>

@HaKuNa Can you share the source?

/Mike
