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):

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?