Kinda stuck with a REST issue

I have been struggling with a REST config issue for a few days now and after lots of searching and reading I am stuck and could do with one help. FWIW, I am new at this so be kind.

I have configured HA to collect data from my networking cloud service, Aruba Central and after reading, researching and reverse engineering what other people have done, I finally have this working pretty well. However, the Bearer token needs to be renewed every 2 hours and this is my challenge. To renew the Bearer token I need to send a POST request along with some credentials (client_id, client_secret, grant_type and refresh_token) to the API in the form of a payload. In return I get a new access_ token and a new refresh_token.

I have tried to do this with the Restful integration, Rest Command and Rest Sensors, with each one failing for a different reason.

RESTful integration - Can’t template the payload

rest:
   - resource_template: https://eu-apigw.central.arubanetworks.com/oauth2/token?client_id={{ states('input_text.central_client_id') }}&client_secret={{ states('input_text.central_client_secret') }}&grant_type=refresh_token&refresh_token{{ states('input_text.central_refresh_token') }}
    method: POST
     scan_interval: 5400
     headers:
       accept: application/json
       content-type: application/json
     sensor:
       - name: "Central New Refresh Token"
         value_template: "{{ value_json.refresh_token }}"
       - name: "Central New Token Type"
         value_template: "{{ value_json.token_type }}"
       - name: "Central New Access Token"
         value_template: "{{ value_json.access_token }}"
       - name: "Central New Token Expires"
         value_template: "{{ value_json.expires_in }}"

RESTful Command - Can template the payload and this seems to refresh the token but I can’t access the response.

rest_command:
  token_refresh:
    url:  https://eu-apigw.central.arubanetworks.com/oauth2/token
    method: POST
    headers:
      accept: application/json
      content-type: application/json
    payload: '{"grant_type":"{{grant_type}}", "refresh_token":"{{refresh_token}}", "client_id":"{{client_id}}", "client_secret":"{{client_secret}}" }'

RESTful Sensor - Can’t template the payload

sensor:
   - platform: rest
     name: "Central API Tokens"
     resource:  https://eu-apigw.central.arubanetworks.com/oauth2/token
     method: POST
     scan_interval: 5400
     headers:
       accept: application/json
       content-type: application/json
     payload: '{"grant_type":"refresh_token", "refresh_token":"{{refresh_token}}", "client_id":"{{client_id}}", "client_secret":"{{client_secret}}" }'
     value_template: '{{ value_json.refresh_token}}'
     json_attributes:
       - access_token
       - token_type
       - expires_in

Any suggestions?

Thanks

Simon

add a space before method

rest:
   - resource_template: https://eu-apigw.central.arubanetworks.com/oauth2/token?client_id={{ states('input_text.central_client_id') }}&client_secret={{ states('input_text.central_client_secret') }}&grant_type=refresh_token&refresh_token{{ states('input_text.central_refresh_token') }}
     method: POST
     scan_interval: 5400
     headers:
       accept: application/json
       content-type: application/json
     sensor:
       - name: "Central New Refresh Token"
         value_template: "{{ value_json.refresh_token }}"
       - name: "Central New Token Type"
         value_template: "{{ value_json.token_type }}"
       - name: "Central New Access Token"
         value_template: "{{ value_json.access_token }}"
       - name: "Central New Token Expires"
         value_template: "{{ value_json.expires_in }}"

Thanks for the response. I think that was just a pasting error on my part. I double checked the code in configuration.yaml and it now matches. Restart HA and then I get the following

2023-08-14 15:33:41.770 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'refresh_token' when rendering '{{ value_json.refresh_token }}'
2023-08-14 15:33:41.773 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'token_type' when rendering '{{ value_json.token_type }}'
2023-08-14 15:33:41.776 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'access_token' when rendering '{{ value_json.access_token }}'

I don’t think the REST post gets through or is accepted because the old access_token is still valid. I can’t find any info on whether this format is OK for the resource_template?

Do you have those input texts with the correct information in them?

Double checked and all look OK. Restart HA and same errors.

Template variable warning: 'dict object' has no attribute 'refresh_token' when rendering '{{ value_json.refresh_token }}'
Template variable warning: 'dict object' has no attribute 'token_type' when rendering '{{ value_json.token_type }}'
Template variable warning: 'dict object' has no attribute 'access_token' when rendering '{{ value_json.access_token }}'
Template variable warning: 'dict object' has no attribute 'expires_in' when rendering '{{ value_json.expires_in }}'

Excuse my ignorance but is there anything different between using the resource_template to hold the payload and the payload option?

There shouldn’t be a difference. With the resource template, you can copy/paste what rest is using and see the results directly in a web browser.

I pasted the resource template into the developers tool template, then pasted the resolved url into Firefox and got the following result

	
error_description	"The access token is missing"
error	"invalid_request"

The thing is, the access token is not needed for the token refresh. It appears the server is seeing this URL as a GET rather than a POST?

How do you tell the server is a POST? It seems to be ignoring the method: POST line.

well when you paste it into an url, it’s probably defaulting to get.

You can turn on debug logging for rest to see whats actually happening. It may already tell you this info in your logs.

Thanks, debug logging helped me find a missing =

The errors are gone and the value_templates now populate. However, they’re the old values. I am going to wait for the scan_interval to elapse and see what it grabs next time round. Fingers crossed.

It is missing a = after refresh_token

Thanks both. The missing = was the issue.