Rest Service Authentication Tokens

I have another server which has a rest service that I want some data from.

I am just wondering how to get and store the authorization tokens as they expire quite quickly.

e.g. Here is my sensor:

sensor:
  - platform: rest
    name: Uninvoiced Spend
    resource: http://192.168.1.13:8081/app/rest/v2/services/someService
    value_template: '{{ value_json|float|round(2) }}'
    unit_of_measurement: "c"
    headers:
      Authorization: !secret my_other_servers_token

How can I get the token to automatically update in HA? Can I set up some sort of automation or something?

Here is the post I need to do to get the Token:


POST /app/rest/v2/oauth/token? HTTP/1.1
Host: 192.168.1.13:8081
Authorization: Basic Y2xpZW50OnNlY3JldA==
content_type: application/x-www-form-urlencoded

grant_type=password&username=XXXXX&password=XXXXXXXXXX



or with Curl

curl --location --request POST 'http://192.168.1.13:8081/app/rest/v2/oauth/token' \
--header 'Authorization: Basic Y2xpZW50OnNlY3JldA==' \
--header 'content_type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=XXXXXX' \
--data-urlencode 'password=XXXXXXXXXXX'