Extend API token lifetime

nick076

Member
Jan 13, 2020
6
0
21
48
Is there a way to extend token(ticket) time as currently is set to 2 hours.
I am gathering information from API , like CPU usage, Memory etc.. and creating sensors on Home Assistant which gets updated every minute.
I am trying to automate it.
Thanks
 
hi,

you get a new ticket again for 2 hours if you reauthenticate
 
hi,

you get a new ticket again for 2 hours if you reauthenticate
I am using this command , per docs:
Code:
curl -k -d "username=root@pam&password=yourpassword" https://10.0.0.1:8006/api2/json/access/ticket
.
then i copy paste Token to my Rest command :
Code:
- platform: rest
  resource: https://x.x.x.x:8006/api2/json/nodes
  name: API TEST
  verify_ssl: false
  headers:
    Cookie: 'PVEAuthCookie=PVE:root@pam:5E1BAB79::Cxmr3rm3SRDw....'
    content-Type: application/json
  method: GET

I am wondering if i can automate Re-authentication as you stated.
Thanks
 
you don't need to use curl. you can use anything else to make the initial request for authentication, save the ticket, and use it afterwards. (most scripting languages will do the job. you can even do it in bash by saving the ticket after running curl)
 
you don't need to use curl. you can use anything else to make the initial request for authentication, save the ticket, and use it afterwards. (most scripting languages will do the job. you can even do it in bash by saving the ticket after running curl)

So making sure i'm following you correctly:
1. i can use current token to query for API
2. after 2 hours i should create a script to do initial request for authentication (is this command for new ticket that goes here :
https://10.0.0.1:8006/api2/json/access/ticket ?)
3. with generated new ticket or cookie and should add that to my command which generates queries toward API

Apologies if stupid question, i am complete amateur in scripting.
 
1. i can use current token to query for API
2. after 2 hours i should create a script to do initial request for authentication (is this command for new ticket that goes here :
https://10.0.0.1:8006/api2/json/access/ticket ?)
3. with generated new ticket or cookie and should add that to my command which generates queries toward API
1. yes

2. for example you can write a method to authenticate to the access/ticket endpoint with your username & password and return the API ticket. also save the timestamp when you retrieve the ticket so you can renew it before 2 hours end. before each request you make call this method and check if the ticket is older than 1 hour for example. if yes reauth and return the new ticket.

3. yes
 
  • Like
Reactions: nick076
forgot to mention that you can also use your old ticket while reauthenticating with the endpoint ( to get a new ticket ).

that way you don't have to save your password
 
1. yes

2. for example you can write a method to authenticate to the access/ticket endpoint with your username & password and return the API ticket. also save the timestamp when you retrieve the ticket so you can renew it before 2 hours end. before each request you make call this method and check if the ticket is older than 1 hour for example. if yes reauth and return the new ticket.

3. yes
Thank you
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!