arcpy.SignInToPortal token expires after 12 hours

1020
5
Jump to solution
06-09-2021 05:59 AM
NeilEtheridge
New Contributor III

I have a script that runs for a long time (2 days) for deploying a large Utility Network.  The script makes a connection to portal early on using the SignInToPortal function, however by the time subsequent commands are run I get a ...

Error: ERROR 002119: Must be connected and signed into the portal.
Failed to execute (AssetPackageToUtilityNetwork).

I checked the token returned by SignInToPortal and it expires 12 hours after the request.  Using Fidder to capture the request I can see "expiration" is part of the request.

/portal/sharing/rest/generateToken/username=utilitynetworkowner&password=abcdefg&expiration=720&client=referer&referer=http://www.esri.com/AGO/B90FF998-17C8-4248-8F0A-A7000000000

Is there anyway to change the timeout request?

 

Thanks,

Neil

0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

In one of my projects I refresh token (if needed) before using token in some operation. I do it a little bit earlier than token expires.

View solution in original post

5 Replies
JakeSkinner
Esri Esteemed Contributor

@NeilEtheridge  you could generate the token with the below code, which includes the expiration parameter.  This value is in minutes, and the max is 15 days:

import requests, json

# Disable warnings
requests.packages.urllib3.disable_warnings()

username = "portaladmin"
password = "********"

tokenURL = 'https://portal.esri.com:7443/arcgis/sharing/rest/generateToken/'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'https://portal.esri.com', 'expiration': 21600}

r = requests.post(tokenURL, data = params, verify=False)
response = json.loads(r.content)
token = response['token']
print(token)
NeilEtheridge
New Contributor III

Thanks Jake for the reply - I should have thought to try it this way as I already do similar in other scripts when making direct calls to the REST interface.  I wasn't sure if subsequent Utility Network commands would recognise this token so went with the down the same track as Gintautas has suggested below.

0 Kudos
ThomasM
New Contributor III

Jake,

Is connecting to AGOL a similar process? I've got a code that is iteratively exporting each Hosted Feature Service in the organization to FGDB, but it tends to hand up after a couple hours. I'm doing a simple

gis = GIS(portalURL, portalUser, portalPass)

to access the AGOL org. Does that generate a token with a time limit? If so, any way to tell how long that time limit is (or to change it)?

 

Thank you!

GIS Specialist - MO Office of Geospatial Information
0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi,

In one of my projects I refresh token (if needed) before using token in some operation. I do it a little bit earlier than token expires.

NeilEtheridge
New Contributor III

Thanks Gintautas.  This was the approach went with ... just checking the token hadn't expired before I needed it again.

Cheers,

Neil

0 Kudos