<jupyter_start><jupyter_text>ArcGIS API for Python Demo: Authentication Options The ArcGIS API for Python has a number of ways to authenticate with your web GIS. This demo provides a way to store commonly used Portals and Organizations to quickly authenticate and start working.  Anonymous Users Let's connect in the simplest way possible - instantiating a GIS object to the default GIS, ArcGIS Online.<jupyter_code>import arcgis
gis = arcgis.gis.GIS()
gis.content.search("1=1")<jupyter_output><empty_output><jupyter_text>Built-in Users<jupyter_code>gis = arcgis.gis.GIS(username="Anieto_esrifederal")<jupyter_output><empty_output><jupyter_text>Setting up a dictionary of GIS URLsNow, let's take a look at a few other options to connect to other GIS organizations and Portals...<jupyter_code># Let's import a few new modules to help us handle passwords securely
from IPython.display import display
import getpass
import os<jupyter_output><empty_output><jupyter_text>I'm going to use a Python dictionary to store the urls and other information for each Web GIS<jupyter_code>portals_dict = {
    "esrifederal_gis": r"https://esrifederal.maps.arcgis.com",
    "natgov_gis": r"http://esri-natgov105.eastus.cloudapp.azure.com/arcgis",
    "dot_gis": r"http://dot.esri.com/portal",
    "dev_gis": r"http://ngse-dev.eastus.cloudapp.azure.com/portal",
    "bill_gis": r"https://idt.esri.com/portal/home/index.html",
}<jupyter_output><empty_output><jupyter_text>Authenticating with any Portal This cell establishes the Web GIS that I want to connect to. I don't need to remember a full URL, just a generic name.<jupyter_code>gis_url = portals_dict["bill_gis"]<jupyter_output><empty_output><jupyter_text>The API allows you to safely pass your authentication details<jupyter_code>gis_username = "anieto"
gis = arcgis.gis.GIS(gis_url, gis_username, verify_cert=False)
gis.users.search()<jupyter_output><empty_output><jupyter_text>Enterprise Identity Store<jupyter_code>print("\n\nPortal-tier Authentication with LDAP - enterprise user")
gisldap = arcgis.gis.GIS("https://portalname.domain.com/webadapter_name", "AVWORLD\\Publisher", "password")
print("Logged in as: " + gisldap.properties.user.username)<jupyter_output><empty_output><jupyter_text>Authenticating to an Organization with Single Sign OnIf we want to authenticate with an ArcGIS Online organization and an Okta username, use the following snippet:<jupyter_code>gis_url = portals_dict["esrifederal_gis"]<jupyter_output><empty_output><jupyter_text>This cell does something slightly more interesting. In our esrifederal organization, we authenticate using Okta. To allow the API to authenticate I've created an app on that organization and received an App ID. I can then pass this to the GIS method's client_id parameter and authenticate. Retrieve App ID from: http://esrifederal.maps.arcgis.com/home/item.html?id=70852f2640e14a5f8050be8ac538e291settings<jupyter_code>gis = arcgis.gis.GIS("https://idt.esri.com/portal", client_id="KZTe9Eyy96GYQAQL")
gis
import getpass
if gis_url == portals_dict["esrifederal_gis"]:
    gis_app_id = getpass.getpass(prompt="App ID: ")
    print("Attempting to log in to '{0}'...".format(gis_url))
    gis = arcgis.gis.GIS(gis_url, client_id=gis_app_id)
    print("Successfully logged in as: " + gis.properties.user.username)
else:
    gis_username = getpass.getpass(prompt="Username: ")
    print("Attempting to log in to '{0}'...".format(gis_url))
    gis = arcgis.gis.GIS(gis_url, gis_username, gis_pw, verify_cert=False)
    print("Successfully logged in as: " + gis.properties.user.username)
gis.content.search("1=1")<jupyter_output><empty_output><jupyter_text>Connecting through ArcGIS Pro<jupyter_code>print("\n\nActive Portal in ArcGIS Pro")  
gis = arcgis.gis.GIS("pro")<jupyter_output><empty_output><jupyter_text>Storing your credentialls locally If you are signing in frequently to a particular GIS and would like to store the credentials locally on your computer, then you could do so using the profile parameter of your GIS.Persistent profiles for the GIS can be created by giving the GIS authorization credentials and specifying a profile name. The profile stores all of the authorization credentials (except the password) in the user’s home directory in an unencrypted config file named .arcgisprofile. The profile securely stores the password in an O.S. specific password manager through the keyring python module. (Note: Linux systems may need additional software installed and configured for proper security) Once a profile has been saved, passing the profile parameter by itself uses the authorization credentials saved in the configuration file/password manager by that profile name. Multiple profiles can be created and used in parallel.<jupyter_code>#Define 2 different profiles for two different urls
playground_gis = GIS(url="https://python.playground.esri.com/portal", username='arcgis_python', password='<PASSWORD>',
                     profile='python_playground_prof')
agol_gis = GIS(url="https://arcgis.com/", username='arcgis_python', password="<PASSWORD>",
    profile="AGOL_prof")
print("profile defined for {}".format(playground_gis))
print("profile defined for {}".format(agol_gis))
def print_profile_info(gis):
    print("Successfully logged into '{}' via the '{}' user".format(
           gis.properties.portalHostname,
           gis.properties.user.username)) 

playground_gis = GIS(profile='python_playground_prof')
print_profile_info(playground_gis)

agol_gis = GIS(profile="AGOL_prof")
print_profile_info(agol_gis)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09683794466403162, 'fraction_numerical': 0.0064678404599353215, 'mean_word_length': 6.029040404040404, 'pattern_counts': {'":': 5, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '608816', 'license': 'Apache-2.0', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1757, 'n_words': 507, 'path': 'talks/uc2018/ExperienceArcGISinNotebooks/3_ Authentication Options.ipynb', 'repo_name': 'nitz21/arcpy', 'size': '5575'}