Authenticate Portal Access when running automated python script

297
1
06-16-2023 12:53 PM
CaseyJost
New Contributor

I have a script which pushes a CSV to the Enterprise Portal to update the existing item. 

The whole process is launched from the Windows Task Scheduler located on a shared computer. The computer is accessed remotely by many members of the team, each logging in with their own user names and passwords. The script is set up to use the ArcGIS API and the current method of authentication is gis=GIS("home"), which I believe uses the credentials of the current ArcPro session. 

I have noticed that the process fails after two weeks, which I  believe is when the access token generated by the ArcPro session expires. I have no need to log into the shared computer regularly, and it is the only machine with Pro installed locally.  Ideally I would not have to remote in to the computer and open ArcPro every two weeks in order to generate a new token, for my process to run as scheduled.

Is there a more effective method of passing authentication? Potentially something that can run indefinitely without a token expiring?  I am looking for a security conscious solution, without having to store username or password in the script.  

Thoughts are greatly appreciated! 

0 Kudos
1 Reply
DonMorrison1
Occasional Contributor III

I solved this problem by storing my arcgis credentials in the keyring associated with my windows user id.  The steps are:

  1. Sign into the windows system under the same windows ID that your script run under
  2. Store the arcgis credentials in the keyring. This is how I do it from the DOS window

 

 

SET PYTHON_EXE=<path to your python.exe>
%PYTHON_EXE%  -c "import keyring; keyring.set_password('<keyring id - used to retrieve password>', '<your arcgis user id>', '<your arcgis password>')"

 

 

  • In your script you can now securely (only code running under your windows ID has access to the keyring) to retrieve those credentials and use them to sign into GIS instead of using "home" 

 

 

arcgis_user = <your arcgis user id>
arcgis_pw  = keyring.get_password('<keyring id - used to retrieve password>', arcgis_user)​

 

 

You will have to remember to update the keyring by running the DOS commands whenever you change your password.  The 'keyring id' can by any string that you make up.