Get the token using ArcGIS Python API

15300
16
Jump to solution
03-13-2018 10:59 PM
simoxu
by MVP Regular Contributor
MVP Regular Contributor

Hello folks,

Can anyone give me a clue how to access token using ArcGIS Python API?

I believe a token was generated when instantiating the GIS class as follows:

from arcgis.gis import GIS

gis=GIS(url="https://...",username="",password="")

I thought the token should be stored somewhere in the gis object, I looked up and down the list of properties and methods but nowhere can I find it. 

Any advices and suggestions are highly appreciated.

Thanks

Simo

Tags (2)
0 Kudos
16 Replies
DCWORK_GIS
New Contributor III

That does work, but in my case, my goal is to use this script, and others like it in AGOL Notebooks, and my org doesn't pay for ArcPy there. So I'm limited to only using the API for Python.

0 Kudos
sredar
by
New Contributor II

What the heck, now I am getting the same thing you are with 

token = gis._con.token
 
Its returns None and just yesterday it worked
 
In a Notebook it does works but it no longer  my in local IDE. 
DCWORK_GIS
New Contributor III

But yesterday it did? That's really weird. I noticed it after we upgraded from 2.7 to 3.2, but maybe it's something else. ESRI's been non-responsive about the issue. Maybe it's a bug.

0 Kudos
sredar
by
New Contributor II

The issue appears to be related to how you access the 'gis', this works:

gis = arcgis.GIS(url='https://{org}.maps.arcgis.com/', username=user, password=pswrd)

token = gis._con.token

this does not:

gis = arcgis.GIS("pro")

token = gis._con.token

 

 

vpelleritoCritigen
New Contributor II

This is bad... Now, even though the user is logged into the Portal in Pro, you must pass those same credentials hardcoded in your Python script.
Thanks, @sredar for discovering this.

JosephRhodes2
Occasional Contributor II

Same issue here. @sredar's workaround is great for my current project (thanks), but there are lots of other cases where arcpy isn't available and I need to get an access token from the GIS object. 

0 Kudos
Matthew_Muehlhauser
New Contributor III

This is another option for getting the token without needing to store the credentials in the script, but it does require some initial setup on the computer that you plan to run the process on because you need to create a profile that will be used for all your future scripts.

1st Run:

gis = GIS("https://www.arcgis.com/", "USERNAME", "PASSWORD", profile="profile_name")
 
Future Runs:
gis = GIS(profile="profile_name")
 
With this, you should be able to access the token using gis._con.token
 
Note: The credentials should be stored at 
Control Panel\User Accounts\Manage your Credentials\Windows Credentials