Python Add Service to AGOL with Stored Credentials?

547
2
Jump to solution
12-09-2019 02:22 PM
Amanda__Huber
Regular Contributor

How can we add a service to AGOL with stored credentials using the python api?

Ideally we would like to add an item from our Portal ArcGIS Server to AGOL though python. 

We've looked through the documentation found publish(), add(), and register(), but did not see anything about storing credentials?
Thanks!
0 Kudos
1 Solution

Accepted Solutions
Amanda__Huber
Regular Contributor

Just follow up on this post in case someone else stumbles upon it with the same question. 

We opened an Esri case and determined this solution:

from arcgis.gis import GIS

 

gis = GIS('https://www.arcgis.com','arch0000')

token = gis._con.token

 

item_properties = {

 

"title":"TestAdd2",

 

"tags":"test",

"url":"https://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire_secure/FeatureServer",

 

"type":"Feature Service",

"description":"Test Me",

"serviceUsername":"user1",

"servicePassword":"user1",

"token":token

 

}

 

gis.content.add(data="https://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire_secure/FeatureServer",owner="arch0000",item_properties=item_properties)

Best, 

Amanda

View solution in original post

2 Replies
Amanda__Huber
Regular Contributor

Just follow up on this post in case someone else stumbles upon it with the same question. 

We opened an Esri case and determined this solution:

from arcgis.gis import GIS

 

gis = GIS('https://www.arcgis.com','arch0000')

token = gis._con.token

 

item_properties = {

 

"title":"TestAdd2",

 

"tags":"test",

"url":"https://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire_secure/FeatureServer",

 

"type":"Feature Service",

"description":"Test Me",

"serviceUsername":"user1",

"servicePassword":"user1",

"token":token

 

}

 

gis.content.add(data="https://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire_secure/FeatureServer",owner="arch0000",item_properties=item_properties)

Best, 

Amanda

JustinReynolds
Occasional Contributor II

Amanda's Post Helped me out a lot.  For anyone looking for another working example for Adding Portal Items to ArcGIS Online with Stored Creds...

# Import the GIS
from arcgis.gis import GIS

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

agol_user = agol.properties.user.username
agol_token = agol._con.token
print("--->Logged into AGOL as {}".format(agol_user))

# Login to Enterprise Portal

# Using Pro Instance for Example
portal = GIS("pro")
portal_user = portal.properties.user.username
print("--->Logged into Portal as {}".format(portal_user))

# Generate a List of Items to Add to AGOL from Portal
# --> Search your Portal for whatever content you want

# Example Search
item_types = []
project_number = "12345"
# Return only the item types of interest in the search
item_types = ['Feature Service', 'Vector Tile Service', 'Tile Servie']

# A search using list comprehension to fine tune the search
items_to_add = [item for item in portal.content.search(query=""+ 
                project_number + "* AND owner:" + portal_user, max_items=100)
               if item.type in item_types]

# Add the Portal Items to AGOL
for item in items_to_add:
    # Check if item already exists
    if agol.content.search(item.title):
        print("{} is already an item in AGOL.".format(item.title))
        continue
    
    item_properties = {"title": item.title,
                       "tags": "tag1, tag2",
                       "url": item.url,
                       "type": item.type,
                       "description": "This is a Portal Item",
                       "serviceUsername": "a_portal_user",
                       "servicePassword": "the_portal_users_password",
                       "token": agol_token
                       }
    data = item.url
    owner = agol_user
    item_properties=item_properties

    new_item = agol.content.add(data=data, owner=owner, 
                                item_properties=item_properties)
    print(new_item)

 

- Justin Reynolds, PE
0 Kudos