Using Python to generate access token for an account that uses organization login

6938
11
01-13-2022 01:22 PM
FelipeDias
New Contributor II

Hi there!

I've been trying to generate a REST API access token for an item on ArcGIS online through a basic Python script and I'm having a hard time.

Here are the URLs of the item that I want to access:

Note: You won't be able to access the item above because it's not shared publicly (which is why I want to generate an access token in the first place). 

I followed the instructions found here (Generate Token in REST) and arrived at the following Python code (substituting the placeholders with my actual username and password):

 

 

import requests
tokenURL = 'https://services9.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 
          'username': 'placeholder_username', 
          'password': 'placeholder_password', 
          'referer': 'http://www.arcgis.com'}
response = requests.post(tokenURL, data = params, verify = False)
token = response.json()['token']

 

 

 

 

The problem is that the code above doesn't work. When I investigate the response object, I notice two things: 

  • It doesn't have a "token" key.
  • It tells me that the URL I used is invalid.

 

 

 

print(response.json()) 
# {'error': {'code': 400, 'message': 'Invalid URL', 'details': ['Invalid URL']}}

 

 

 

So I tried using tokenURL='https://www.arcgis.com/sharing/rest/generateToken', but that didn't work either: 

 

 

 

print(response.json()) 
# {'error': {'code': 400, 'message': 'Unable to generate token.', 'details': ['Invalid username or password.']}}

 

 

 

 I believe this might be because I typically log into ArcGIS online through my organization's URL: mycompany.maps.arcgis.com (as seen below):

 

FelipeDias_0-1677171213329.png

 

So I finally tried to use tokenURL='https://mycompany.maps.arcgis.com/sharing/rest/generateToken', but that didn't work either:

 

 

 

print(response.json()) 
# {'error': {'code': 400, # 'message': 'Unable to generate token.', # 'details': ['Invalid username or password.']}}

 

 

 

Does anyone know what I need to do to successfully generate an access token for the item above? 

0 Kudos
11 Replies
DCWORK_GIS
New Contributor III

Hi Mark, would you mind sharing your method of accessing the token with a built-in user, not using user/pass. I had been accessing it using: 

gis=GIS('home')
token=gis._con.token

but that code isn't working anymore, and I'm at a loss to find why, or a workaround.

 

0 Kudos
MarkGambordella
New Contributor III

I was able to find a work around for now.  you cannot use your SAML account but instead have to create a built-in account.  Use the built-in account for your user:password in the geoprocessing tool found in the link below.  the tool creates a URL for only the first attachment (which is the only one I needed), the URL with a token is added to the attribute table.  because the token expires, you can then run a script to update the token once a week.  the links below were helpful in setting it up.  

Show Attachments in Web Map Popup - Esri Community

Attachments to Popup - YouTube

Schedule a Python Script using Windows Task Scheduler - YouTube

0 Kudos