ArcGIS Python API – Resolving “Unable to Generate Token” Error

arcgis-enterprisearcgis-onlinearcgis-python-api

I've been trying to log into either my ArcGIS Online or ArcGIS Enterprise portal through the ArcGIS Python API.

Using the GIS module, I have tried the following:

gis = GIS("https://subdomain.example.com/portal", "<username>", "<password>")
gis = GIS("https://subdomain.example.com:7443/arcgis/home", "<username>", "<password>")
gis = GIS("https://www.arcgis.com/", "<username>", "<password>")

All of the above return the following error in the Jupyter notebook:

    ---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
In  [2]:
Line 1:     gis = GIS("https://www.arcgis.com/", "<username>", "<password>")

File C:\Users\bilegjargal.MONMAP\AppData\Local\ESRI\conda\envs\arcgispro_clone\lib\site-packages\arcgis\gis\__init__.py, in __init__:
Line 483:   raise e

File C:\Users\bilegjargal.MONMAP\AppData\Local\ESRI\conda\envs\arcgispro_clone\lib\site-packages\arcgis\gis\__init__.py, in __init__:
Line 444:   proxy=kwargs.get("proxy", None),

File C:\Users\bilegjargal.MONMAP\AppData\Local\ESRI\conda\envs\arcgispro_clone\lib\site-packages\arcgis\gis\_impl\_portalpy.py, in __init__:
Line 205:   self.get_properties(True)

File C:\Users\bilegjargal.MONMAP\AppData\Local\ESRI\conda\envs\arcgispro_clone\lib\site-packages\arcgis\gis\_impl\_portalpy.py, in get_properties:
Line 1227:  raise e

File C:\Users\bilegjargal.MONMAP\AppData\Local\ESRI\conda\envs\arcgispro_clone\lib\site-packages\arcgis\gis\_impl\_portalpy.py, in get_properties:
Line 1207:  resp = self.con.post(path, self._postdata(), ssl=True)

File C:\Users\bilegjargal.MONMAP\AppData\Local\ESRI\conda\envs\arcgispro_clone\lib\site-packages\arcgis\gis\_impl\_con\_connection.py, in post:
Line 966:   elif token_as_header == False and self.token is not None:  # as ?token=

File C:\Users\bilegjargal.MONMAP\AppData\Local\ESRI\conda\envs\arcgispro_clone\lib\site-packages\arcgis\gis\_impl\_con\_connection.py, in token:
Line 1447:  self._token = self._enterprise_token()

File C:\Users\bilegjargal.MONMAP\AppData\Local\ESRI\conda\envs\arcgispro_clone\lib\site-packages\arcgis\gis\_impl\_con\_connection.py, in _enterprise_token:
Line 1591:  res = self.post(path=self._token_url, params=postdata, add_token=False)

File C:\Users\bilegjargal.MONMAP\AppData\Local\ESRI\conda\envs\arcgispro_clone\lib\site-packages\arcgis\gis\_impl\_con\_connection.py, in post:
Line 1079:  force_bytes=kwargs.pop("force_bytes", False),

File C:\Users\bilegjargal.MONMAP\AppData\Local\ESRI\conda\envs\arcgispro_clone\lib\site-packages\arcgis\gis\_impl\_con\_connection.py, in _handle_response:
Line 625:   self._handle_json_error(data["error"], errorcode)

File C:\Users\bilegjargal.MONMAP\AppData\Local\ESRI\conda\envs\arcgispro_clone\lib\site-packages\arcgis\gis\_impl\_con\_connection.py, in _handle_json_error:
Line 648:   raise Exception(errormessage)

Exception: Unable to generate token.
Invalid username or password.
(Error Code: 400)
---------------------------------------------------------------------------

I have tried this on a separate Anaconda environment as well as from inside ArcGIS Pro.

What could be wrong here?

EDIT: From ArcGIS Online Notebooks, running:

gis = GIS('home')

doesn't return an error, but a warning:

/opt/conda/lib/python3.7/site-packages/arcgis/gis/__init__.py:575: UserWarning:

You are logged on as <username> with an administrator role, proceed with caution.

Specifying the Online portal and my username/password returns the same error on the ArcGIS Online notebook.

Best Answer

Found what I was doing wrong.

Just found out that the username is case sensitive.

If the username is "User" then specifying username as "user" will not work.

Related Question