arcgis-python-api icon indicating copy to clipboard operation
arcgis-python-api copied to clipboard

APIKeyManager.get() and .keys methods fail to retrieve existing API keys in ArcGIS Python API 2.4.0

Open pmd84 opened this issue 8 months ago • 0 comments
trafficstars

Describe the bug

The APIKeyManager.get() method and keys property don't correctly retrieve existing API keys, despite documentation suggesting these should return valid API keys. The API keys exist and can be found through content search, but the dedicated API key retrieval methods return None or empty lists.

To Reproduce

Steps to reproduce the behavior:

import arcgis
from arcgis.gis import GIS

# Connect to ArcGIS Online
gis = GIS("https://www.arcgis.com", "username", "password")

# First search for API keys to confirm they exist
api_keys = gis.content.search(query="", item_type="Application", 
                             max_items=10, 
                             outside_org=False)
api_key = [k for k in api_keys if "APIToken" in k.typeKeywords][0]
print(f"API Key found via content search: {api_key.title} (ID: {api_key.id})")

# Try to retrieve the same API key using documented methods
print("\nAttempting API Key retrieval using get() method")
key_by_title = gis.api_keys.get(title=api_key.title)
print(f"Result: {key_by_title}")  # Returns None

# Try using APIKeyManager directly
print("\nAttempting retrieval via APIKeyManager")
api_key_manager = arcgis.gis._impl.APIKeyManager(gis)
keys_list = api_key_manager.keys
print(f"API keys found: {len(keys_list)}")  # Returns 0 even when keys exist

Expected behavior

The get() method should return an API key when given a valid title, and the keys property should return a list of all available API keys for the authenticated user.

Workaround

The only working approach is to manually instantiate an APIKey class with an item found through content search:

# Workaround: create API key class directly
from arcgis.gis._impl import APIKey
api_key_class = APIKey(api_key, gis)
print(f"Successfully created API Key class via workaround: {api_key_class}")

Platform

  • OS: Databricks
  • Python API Version: 2.4.0

Additional context

The official documentation at https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#apikeymanager suggests these methods should work as expected, but they don't seem to function correctly in version 2.4.0.

pmd84 avatar Mar 11 '25 13:03 pmd84