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:

  - platform: rest
    name: Uninvoiced Spend
    value_template: '{{ value_json|float|round(2) }}'
    unit_of_measurement: "c"
      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
Authorization: Basic Y2xpZW50OnNlY3JldA==
content_type: application/x-www-form-urlencoded


or with Curl

curl --location --request POST '' \
--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'