How to increase time before token expires with Arcgis Python API?

1129
3
06-17-2020 01:28 PM
NathanielCregut
New Contributor

I know you can increase the time before a token expires when using the Arcgis REST API in the parameters you pass, but can you do the same using the Arcgis Python API? I know how to access the token using 'gis._con.token' and the time beofre expiry with 'gis._con._expiration', but is there somewhere I can increase the time before expiration to higher than the default 60 minutes via the python API? Or should I simply re-authenticate my credentials using 'GIS(portal, username, password)' after 60 minutes?

0 Kudos
3 Replies
VictorTey
Esri Contributor

I would re-authenticate. Having a long expiry token can be considered a security risk.

However saying that, you can also get the token from the rest end point and pass in that token in your GIS. Only for build in users though.

JoshuaBixby
MVP Esteemed Contributor

Maximum expiration time for tokens is set on the server, are you sure the server allows longer than 60 minutes?  If not, any request for longer time will simply return 60-minute token.

0 Kudos
PaulBatley
New Contributor III

I've got the same issue.. trying to run Portal site exports from the backend. The export takes around 65 minutes so I can't move on with the same token to export the Hosting site. Looks like you can't set the token timeout within the Python API - https://developers.arcgis.com/python/api-reference/1.6.2/arcgis.gis.toc.html#gis On the generate token page it defaults to 1h and then options from there. But the Class doesn't have to extend the log in period. Would have been a nice to have. 

10-05-2021 20:20:41 -- DEBUG: Staging Portal site at:- https://server.domain:7443/arcgis/portaladmin exported successfully.
Exported file: 10-May-2021-19-17-13-BST-FULL.portalsite

10-05-2021 20:20:41 -- DEBUG: REQUEST (get): https://server.domain:7443/arcgis/portaladmin/federation/servers?f=json&token=maOGZlpLrHHj_UjxwmPGS_...
10-05-2021 20:20:42 -- INFO: Token expired during get request, fetching a new token and retrying
10-05-2021 20:20:42 -- DEBUG: REQUEST: https://server.domain:7443/arcgis/sharing/rest/, {'f': 'json'}
10-05-2021 20:20:43 -- DEBUG: RESPONSE: https://server.domain:7443/arcgis/sharing/rest/, {"currentVersion":"6.1"}
10-05-2021 20:20:43 -- DEBUG: REQUEST: https://server.domain:7443/arcgis/sharing/rest/generateToken
10-05-2021 20:20:43 -- DEBUG: RESPONSE: https://server.domain:7443/arcgis/sharing/rest/generateToken, {"token":"Z-KPZFAxaB7cGkI_DjXf5J8nokAO1l3f2_K0qR2ZpnNMZeq0xfbD21DOKz5Ksl-ist1Xl2EfuVlxpiSxivGd6YN2KMe1yX9BnQgKDK6gE6_UBmD9FN0xeUwGlMIjsO98","expires":1620678043117,"ssl":true}
10-05-2021 20:20:43 -- DEBUG: REQUEST (get): https://server.domain:7443/arcgis/portaladmin/federation/servers?token=Z-KPZFAxaB7cGkI_DjXf5J8nokAO1...

0 Kudos