Select to view content in your preferred language

Non-interactive GIS connection for Arcgis Online with Python

927
3
01-08-2024 05:27 AM
Labels (1)
julian3930
New Contributor

Hello, 

I use Arcgis online with Python. I used to connect like this:

from arcgis.gis import GIS

gis = GIS(username=XYZ, password=XYZ)

To connect to my Arcgis account, I now use OKTA, so I can no longer use the above method. I've tried using the portal url like this (non-interactive method):

gis = GIS(url=”https://XYZX.maps.arcgis.com”)

 

However, I have two problems:

  1. gis.users.me : this command returns null, so I can no longer use gis.users.me.items
  2. The following code generates a 'NoneType' object is not subscriptable error. The detailed error is shown below.

item_properties = {'title': XYZ, 'snippet': XYZ, 'tags': XYZ}

file = XYZ.csv

gis.content.add(item_properties, data = file, folder = XYZ) 

Do you know how to connect properly with a non-interactive method and how to add files via this connection ? Thank you.

The detailed error : 

/opt/conda/lib/python3.9/site-packages/arcgis/gis/__init__.py in add(self, item_properties, data, thumbnail, metadata, owner, folder, item_id, **kwargs)

   6835             if "text" in kwargs:

   6836                 item_properties["text"] = kwargs.pop("text", None)

-> 6837             itemid = self._portal.add_item(

   6838                 item_properties,

   6839                 data,

 

/opt/conda/lib/python3.9/site-packages/arcgis/gis/_impl/_portalpy.py in add_item(self, item_properties, data, thumbnail, metadata, owner, folder)

    423         # If owner isn't specified, use the logged in user

    424         if not owner:

--> 425             owner = self.logged_in_user()["username"]

    426

    427         # Setup the item path, including the folder, and post to it

 

TypeError: 'NoneType' object is not subscriptable

 

Tags (3)
3 Replies
JakeSkinner
Esri Esteemed Contributor

Hi @julian3930,

With SAML authentication, you won't be able to authenticate without user interaction.  One thing you could try is to create an Application in ArcGIS Online by going to New Item > Application:

JakeSkinner_0-1704725572011.png

Specify Other Application and then specify a name:

JakeSkinner_1-1704725596204.png

In the Application details, this will create a Client ID and Client Secret:

JakeSkinner_2-1704725668545.png

Using these two, you can generate a token to authenticate with AGOL:

import requests, json
from arcgis.gis import GIS

# Variables
clientId = 'Gl2890aGas'
clientSecret = '123kl0987234jajfw9087f123r'

# Generate Token
params = {
    'client_id': clientId,
    'client_secret': clientSecret,
    'grant_type': "client_credentials",
}
request = requests.get('https://www.arcgis.com/sharing/oauth2/token', params=params)
response = request.json()
token = response["access_token"]

# Connect to GIS
gis = GIS('https://www.arcgis.com', token=token)

 

However, you have very limited functionality when authenticating this way.

julian3930
New Contributor

Hello, 

Thank you for your help. Unfortunately, this proposition doesn't work as well. When I use the exact same code changing id and client secret, I have the same error when I do gis.content.add. It seems that it is a problem of the logged user.

/opt/conda/lib/python3.9/site-packages/arcgis/gis/__init__.py in add(self, item_properties, data, thumbnail, metadata, owner, folder, item_id, **kwargs)
6835 if "text" in kwargs:
6836 item_properties["text"] = kwargs.pop("text", None)
-> 6837 itemid = self._portal.add_item(
6838 item_properties,
6839 data,

/opt/conda/lib/python3.9/site-packages/arcgis/gis/_impl/_portalpy.py in add_item(self, item_properties, data, thumbnail, metadata, owner, folder)
423 # If owner isn't specified, use the logged in user
424 if not owner:
--> 425 owner = self.logged_in_user()["username"]
426
427 # Setup the item path, including the folder, and post to it

TypeError: 'NoneType' object is not subscriptable

0 Kudos
NikSandsTP
Occasional Contributor

My solution to this issue is to create a new AGOL account using AGOL credentials, not organisation credentials.  Then the username/password method works fine.  The other methods all have limited functionality, as you've discovered.

NB:  To avoid having credentials hard-coded in your script, see:

Working with different authentication schemes | ArcGIS API for Python

(see the section, 'Storing your credentials locally')

0 Kudos