Esri / arcgis-python-api

Documentation and samples for ArcGIS API for Python
https://developers.arcgis.com/python/
Apache License 2.0
1.88k stars 1.1k forks source link

Connection using GIS("Pro") intermittently fails when running script #1752

Open mappybird opened 8 months ago

mappybird commented 8 months ago

Describe the bug Connection using GIS("Pro") intermittently fails when running script

To Reproduce from arcgis.gis import GIS gis = GIS("Pro")

for loop in loops: Run loop to do something big

Screenshots authentication_pro

Expected behavior Script runs without authentication error

Platform (please complete the following information):

Additional context Just to be clear, this error happens intermittently when using the GIS("pro") method of connecting to ArcGIS Python API. There is also no traceback I can provide as it only outputs the error message above but still continues to run the loop (with more failures due to the GIS not being available anymore).

I can confirm that I'm logged into ArcGIS Pro when running the script and the Active portal is set. It's just odd that it happens intermittently and I was wondering how GIS("Pro") actually authenticates? Is it a matter of expired token and it fails to renew it?

nanaeaubry commented 7 months ago

@mappybird Pro credentials time out every so many days. It depends how frequently your script is run and how often you get the error? Not sure on the exact number of days but I can find out if it helps

mappybird commented 7 months ago

@nanaeaubry Thanks for following up, I use a fresh login to ArcGIS Pro everytime I run the script and I run it manually at the moment, not as a scheduled task.

The script runs fine initially in a loop but it will randomly error out with the authentication error after a while.

I am able to sometimes able to complete running the script with no errors. Worth noting that if I run it using a built-in account, the error does not appear.

I also tried to rule out network issues by running the script on a machine on a different network but the error still persists.

FYI, the script I'm running generates offline managed areas

nanaeaubry commented 7 months ago

@mappybird The login type might affect the longevity of the token. I found this : https://enterprise.arcgis.com/en/portal/latest/administer/windows/specify-the-default-token-expiration-time.htm

You can try to edit the token expiration time to see if it makes a difference? Creating these offline managed areas take a while depending on size so maybe you are running into this issue?

I can reach out to others and see if someone knows more about this behavior

achapkowski commented 7 months ago

@nanaeaubry Thanks for following up, I use a fresh login to ArcGIS Pro everytime I run the script and I run it manually at the moment, not as a scheduled task.

The script runs fine initially in a loop but it will randomly error out with the authentication error after a while.

I am able to sometimes able to complete running the script with no errors. Worth noting that if I run it using a built-in account, the error does not appear.

I also tried to rule out network issues by running the script on a machine on a different network but the error still persists.

FYI, the script I'm running generates offline managed areas

What form of security are you see this time out? To manage offline areas, you have to the owner of that Item. Administrator's can't manage other people's offline areas.

mappybird commented 7 months ago

@nanaeaubry

I can try extending the token time in Portal, I thought the GIS('Pro') authentication method would've handled the negotiation for a token refresh when it returns an expired token but I'm unsure of the actual mechanism.

Like I mentioned above, absolutely no issues when I use a built-in account.

@achapkowski I'm using SAML to authenticate in Pro.

I am the owner of the WebMap Item and I am able to generate offline areas partially depending on when the authentication 'times out'. It's kind of a dice roll to be honest.

achapkowski commented 7 months ago

@nanaeaubry

I can try extending the token time in Portal, I thought the GIS('Pro') authentication method would've handled the negotiation for a token refresh when it returns an expired token but I'm unsure of the actual mechanism.

Like I mentioned above, absolutely no issues when I use a built-in account.

@achapkowski I'm using SAML to authenticate in Pro.

I am the owner of the WebMap Item and I am able to generate offline areas partially depending on when the authentication 'times out'. It's kind of a dice roll to be honest.

@mappybird the issue is most likely with ArcGIS Pro and when it expires it's credentials. You can try using the arcpy method Signin to Portal method and pass your credentials at the top of your script.

https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/signintoportal.htm

mappybird commented 7 months ago

@achapkowski

The SignInToPortal method does not support SAML(or interactive logins).

Am I out of luck here?

mappybird commented 7 months ago

I've done some further testing and and got an update:

Using the following authentication method with a built-in account, it works without any issues : gis = GIS("https://portalname.domain.com/webadapter_name", "sharinguser", "password")

However, when the GIS('Pro') method of authentication was used, it errors outs with the following message regardless if you use a built-in account or SAML ( I initially thought built in might work but still failed when I attempted to run the script):

A general error occurred: Could not login using Pro authencation. Please verify in Pro that you are logged in.

Token expiry is sufficiently high in Portal so I'm a bit perplexed as to why it will fail intermittently.