HOW TO

Add a web layer with stored credentials to ArcGIS Online using ArcGIS API for Python

Last Published: April 25, 2020

Summary

Web layers can be added to ArcGIS Online from an existing ArcGIS REST Services Directory through ArcGIS API for Python using the gis.content.add() function. However, if the desired layer contains a stored credential, the function is hindered and the process of adding the web layer fails.

Procedure

The following steps describe how to add a web layer with stored credentials to ArcGIS Online using ArcGIS API for Python.

  1. Import the necessary modules.
from arcgis.mapping import WebMap
from arcgis.gis import GIS
  1. Connect to the ArcGIS Online account and create a token.
gis = GIS('https://www.arcgis.com','username')

token = gis._con.token
  1. Create the item properties for the desired web layer. The item properties specifies the required credentials to access the web layer.
item_properties = 
{
"title":"TestAdd2", #the same title

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

"type":"Feature Service",
"description":"Test Me",
"serviceUsername":"username",
"servicePassword":"password",
"token":token
}
  1. Add the web layer.
gis.content.add(data="https://sample.arcgisonline.com/",owner="username",item_properties=item_properties)

The following shows the full code:

from arcgis.mapping import WebMap
from arcgis.gis import GIS

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

token = gis._con.token

item_properties = 
{
"title":"TestAdd2", #the same title

"tags":"test",
"url":"https://sample.arcgisonline.com/arcgis/rest/services/Wildfire_secure/FeatureServer",

"type":"Feature Service",
"description":"Test Me",
"serviceUsername":"username",
"servicePassword":"password",
"token":token
}

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

Article ID:000022504

Software:
  • ArcGIS API for Python 1 x
  • ArcGIS Online

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic