Select to view content in your preferred language

Unable to access token using gis._con.token

738
5
Jump to solution
11-27-2023 05:31 PM
DCWORK_GIS
New Contributor III

Hello,
I'm hoping someone can help with this problem I ran into today. I can no longer access the authentication token using the Connect through Built-In account method, or the Connect through Pro method. Both of these methods worked for me up until today, and would return a token string accessed through gis._con.token. I can still get a token through OAuth, but I need to run scheduled scripts, unsupervised. Also, my org doesn't use Named User Authentication, so username/password isn't an option.

// This method still works, and returns a token:

gis = GIS('https:myGisPortal.maps.arcgis.com', client_id='myClientID')
print("Logged in as: " + gis.properties.user.username)
print('token='+str(gis._con.token))

Returns:
Logged in as: myUsername
token=sYzv_5rgSSiElv7...bzoR2oayAHTvuJG8


// But these methods don't work anymore, and do not return a token:

gis = GIS('pro')
print("Logged in as: " + gis.properties.user.username)
print('token='+str(gis._con.token))

Returns:
Logged in as: myUsername
token=None


gis = GIS('home')
print("Logged in as: " + gis.properties.user.username)
print('token='+str(gis._con.token))

Returns:
Logged in as: myUsername
token=None

0 Kudos
1 Solution

Accepted Solutions
JosephRhodes2
Occasional Contributor II

Looks like this is still an issue in ArcGIS Pro 3.3./Python API 2.3.0. If arcpy is available, @Vincentvd's solution works, but we had a need to find a workaround when arcpy wasn't available. 

I've had success using the authenticated session object for REST API and Sharing API calls to secured services instead of passing the token explicitly, like so (only tested on AGOL):

 

import json
from arcgis.gis import GIS

gis = GIS("pro")

# Use the session from the GIS object
session = gis._con._session

# Example secured URL, doesn't have to be admin, just an example
update_definition_url = "https://services8.arcgis.com/XXXXXXXXXXXXXXXX/arcgis/rest/admin/services/XXXXXXXXXXX/FeatureServer/updateDefinition"

# Define some parameters for the update; again, just an example
update_def_params = {
    'updateDefinition': json.dumps({
        "capabilities": "Query,Editing,Create,Update,Delete,Sync"
    }),
    'async': 'false',
    'f': 'json'
}

response = session.post(update_definition_url, data=update_def_params, timeout=10)
result = response.json()
print(result)

 


This apparently provides a subclass of a Requests session, so you can use it in place of requests; e.g., session.get, session.post, etc. Your mileage may vary, but this solved the problem for me in a workflow where arcpy wasn't an option.

View solution in original post

5 Replies
DCWORK_GIS
New Contributor III

To add context, I recently upgraded from AG Pro 2.9 to 3.2. Both authentication methods gis = GIS('home')gis = GIS('pro')  would allow access to the authentication token through  token=gis._con.token in AG Pro 2.9, but this does not seem to be the case anymore in 3.x. 2 coworkers tested it in their 3.2 machines and it didn't work for them, and one tested it in 3.1.2, and it didn't work there either. 

 

 

 

0 Kudos
ChrisDalla_Piazza
New Contributor III

I have the same problem.  To clarify, I am attempting to pass Active Directory credentials to GIS() to our enterprise portal.

gis = GIS('https://portalserver.domain.com/arcgis', 'Domain\\username', 'myPassword')

I know the authentication is succeeding because if I intentionally fudge the password the script fails.

0 Kudos
Vincentvd
New Contributor II

Faced the same problem today with GIS("PRO"). The solution when using arcgis pro seems to be using the following code. I also tested GIS() with a URL to arcgis enterprise with a username and password and that also works.

 

import arcpy

token = arcpy.GetSigninToken()
if token is not None:
    print(token['token'])

 

 Source: GetSigninToken—ArcGIS Pro | Documentation

JosephRhodes2
Occasional Contributor II

Looks like this is still an issue in ArcGIS Pro 3.3./Python API 2.3.0. If arcpy is available, @Vincentvd's solution works, but we had a need to find a workaround when arcpy wasn't available. 

I've had success using the authenticated session object for REST API and Sharing API calls to secured services instead of passing the token explicitly, like so (only tested on AGOL):

 

import json
from arcgis.gis import GIS

gis = GIS("pro")

# Use the session from the GIS object
session = gis._con._session

# Example secured URL, doesn't have to be admin, just an example
update_definition_url = "https://services8.arcgis.com/XXXXXXXXXXXXXXXX/arcgis/rest/admin/services/XXXXXXXXXXX/FeatureServer/updateDefinition"

# Define some parameters for the update; again, just an example
update_def_params = {
    'updateDefinition': json.dumps({
        "capabilities": "Query,Editing,Create,Update,Delete,Sync"
    }),
    'async': 'false',
    'f': 'json'
}

response = session.post(update_definition_url, data=update_def_params, timeout=10)
result = response.json()
print(result)

 


This apparently provides a subclass of a Requests session, so you can use it in place of requests; e.g., session.get, session.post, etc. Your mileage may vary, but this solved the problem for me in a workflow where arcpy wasn't an option.

DCWORK_GIS
New Contributor III

I haven't tested yet but this looks promising. The ArcPy solution is good too, but if you want to use arcPy in a Notebook on AGOL you have to pay extra. Thanks!

0 Kudos