Select to view content in your preferred language

Changing the embedded the credential in ArcGIS Online item page

512
6
Jump to solution
09-11-2024 12:42 PM
Labels (1)
CaitBoyer
Occasional Contributor

Hi everyone! 

Our organization would like to change the Username and Password embedded in 400+ services added as items in ArcGIS Online. This is possible through an item's Settings page under the "Credentials" heading, "To change the stored credentials, enter updated credentials.

However, it will be quite tedious to update each of the 400+ layers individually. Is there a way to change these with the API?

Thank you kindly! 

1 Solution

Accepted Solutions
CaitBoyer
Occasional Contributor

Hi all! 

Checking back in to say we used a smattering of different techniques to get this to work-- at first it was telling me it completed successfully, even though my Server Manager was still telling me it was connecting with the old credential. 

Some notes; the token bit of the official documentation didn't seem necessary for us. Also, I noticed that the server chugged a bit when just forcing through 480 updates in rapid succession, so I added a 3 second sleep between updates to give the server (she's an old one) a little time to rest. Also added some error handling to troubleshoot when something goes wrong. Hope this is helpful, and thank you everyone for your very helpful replies 💕

 

import time
from arcgis.gis import GIS

# Variables
username = 'agolusername'
password = 'agolpassword'
group_id = 'groupIDnumber'  # Replace with your actual group ID
embeddedUsername = 'serverusername'
embeddedPassword = 'serverpassword'

try:
    # Connect to AGOL
    print("Connecting to AGOL...")
    gis = GIS("https://arcgis.com", username=username, password=password)
    print("Connection successful.")

except Exception as e:
    print(f"Failed to connect to AGOL: {e}")
    exit()

# Get the group by ID
group = gis.groups.get(group_id)

# Check if the group was found
if group:
    # Loop through all items in the group
    for item in group.content():
        try:
            # Reference layer
            print(f"Updating item: {item.title} (ID: {item.id})")
            
            # Specify username/password
            item_properties = {
                "url":item['sourceUrl'],
                'serviceUsername': embeddedUsername,
                'servicePassword': embeddedPassword
            }
            
            # Update item
            item.update(item_properties=item_properties)
            print(f"Successfully updated item: {item.title} (ID: {item.id})")
        except Exception as e:
            print(f"Failed to update item: {item.title} (ID: {item.id}). Error: {e}")
        # Pause for 3 seconds before moving on (or else the server will start crashing)
        print("Pausing for 3 seconds...")
        time.sleep(3)
else:
    print(f"Group with ID '{group_id}' not found.")

 

 

View solution in original post

6 Replies
JakeSkinner
Esri Esteemed Contributor

@CaitBoyer below is an example that I believe will do this.  You will just need to iterate through the items that need to be updated, for example, those layers whose URL's contain utility.arcgis.com or typeKeywords contain 'Service Proxy'.

from arcgis.gis import GIS

# Variables
username = 'jskinner_rats'
password = '*******'
itemId = '5bad6ca9233e42e396146f887cefb5e6'
embeddedUsername = 'portaladmin'
embeddedPassword = '********'

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

# Reference layer
item = gis.content.get(itemId)

# Specify username/password
item_properties={
 'serviceUsername': embeddedUsername,
 'servicePassword': embeddedPassword
 }

# Update item
gis_content_item = item.update(item_properties=item_properties)

 

0 Kudos
JACooper
Regular Contributor

I have the same exact issue, and while I can't comment on whether updating service username and password properties worked for me, I found another way that may accomplish this as well.

 

 

from arcgis.gis import GIS
import requests

# Get the GIS object
gis = GIS("https://www.arcgis.com", "your_username", "your_password")

# Get a valid token from the GIS object
token = gis._con.token

# Include the token in the request parameters
update_url = f"{og_delaware_fl.url}/admin"
payload = {
    'username': 'your_username',
    'password': 'your_password',
    'f': 'json',
    'token': token  # Add the token here
}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}

# Send the request
response = requests.post(update_url, data=payload, headers=headers)
print(response.json())

 

 

Tags (1)
ODWC_GIS
Frequent Contributor

@JACooper  found the ESRI Technical Support Article!

https://support.esri.com/en-us/knowledge-base/bulk-update-stored-credentials-for-items-in-portal-for...

I need to get started on this myself...

JACooper
Regular Contributor

Nice! Thank you. Yes, don't wait until it is too late. I got into a real mess when I forgot to reassign before removing organizational members...

0 Kudos
ODWC_GIS
Frequent Contributor

Well, I gave it a shot... It appears that all the items now know that the Username is supposed to be the one used by the Admin (and not defaulting to one of the AGOL Usernames), but they all act like they don't have a password entered.

Since I was panicking, I just typed the PW in manually for each AGOL item.  And that seemed to mostly do it.  There were a couple items that I had to use ArcGIS Online Assistant to copy/paste/save the source service url to make it understand that it was still a working service item.

I know the script from the ESRI Technical page did something, but the immediate result was not in line with my intended goals.

0 Kudos
CaitBoyer
Occasional Contributor

Hi all! 

Checking back in to say we used a smattering of different techniques to get this to work-- at first it was telling me it completed successfully, even though my Server Manager was still telling me it was connecting with the old credential. 

Some notes; the token bit of the official documentation didn't seem necessary for us. Also, I noticed that the server chugged a bit when just forcing through 480 updates in rapid succession, so I added a 3 second sleep between updates to give the server (she's an old one) a little time to rest. Also added some error handling to troubleshoot when something goes wrong. Hope this is helpful, and thank you everyone for your very helpful replies 💕

 

import time
from arcgis.gis import GIS

# Variables
username = 'agolusername'
password = 'agolpassword'
group_id = 'groupIDnumber'  # Replace with your actual group ID
embeddedUsername = 'serverusername'
embeddedPassword = 'serverpassword'

try:
    # Connect to AGOL
    print("Connecting to AGOL...")
    gis = GIS("https://arcgis.com", username=username, password=password)
    print("Connection successful.")

except Exception as e:
    print(f"Failed to connect to AGOL: {e}")
    exit()

# Get the group by ID
group = gis.groups.get(group_id)

# Check if the group was found
if group:
    # Loop through all items in the group
    for item in group.content():
        try:
            # Reference layer
            print(f"Updating item: {item.title} (ID: {item.id})")
            
            # Specify username/password
            item_properties = {
                "url":item['sourceUrl'],
                'serviceUsername': embeddedUsername,
                'servicePassword': embeddedPassword
            }
            
            # Update item
            item.update(item_properties=item_properties)
            print(f"Successfully updated item: {item.title} (ID: {item.id})")
        except Exception as e:
            print(f"Failed to update item: {item.title} (ID: {item.id}). Error: {e}")
        # Pause for 3 seconds before moving on (or else the server will start crashing)
        print("Pausing for 3 seconds...")
        time.sleep(3)
else:
    print(f"Group with ID '{group_id}' not found.")