Skip to Main Content
University of Texas University of Texas Libraries

GIS & Geospatial Data Services

Python Scripts & ArcGIS Online

Getting Started with ArcGIS Online and Python

It is possible to connect to your UT linked ArcGIS Online account using a Python script and the ArcGIS Python API but doing so does require a little preparation. In order to get started with this process you will first need to make sure that you have the arcgis Python package installed on your machine. If you do not have package already installed you can use pip to install it by following the instructions on the package page. Please note that this package requires Python 3.4 or above and, it is compatible with both Windows and MacOS computers which is notable since most ArcGIS software requires the Windows operating system. Based on the way that our UT ArcGIS Online organizational account has been configured, during a typical sign in to UT linked ArcGIS Online account your EID credentials are processed by our UT authentication server which then communicates the results of the sign on attempt to ArcGIS Online. When you are using a Python script to connect to your UT linked ArcGIS Online account, the authentication is slightly different though as you do not directly pass in your UT EID and password. Instead you will need to follow the steps listed below in your ArcGIS Online account to generate a unique application ID that can be used to prove your identify. The steps below show how a client id can be obtained by registering a new application with your GIS. Only one client id is required, so if your GIS has one already, you may use it instead of creating a new application.
  1. Log into your UT linked ArcGIS Online account
  2. Click on the My Content tab
  3. Click + New Item near the top right of the page. In the popup window that appears click Application to specify the type of item you want to create. On the next page in the popup window, select the button for Other Application then click next. On the final page in the popup window fill in the information below.
    Title: Python
    Tags: python
    Summary: python
  4. On the Item details page of this newly created application, navigate to the Settings tab
  5. Click the Registered Info button located near the bottom of the page. This will dispaly an App ID. This is your client id and needs to be passed in as the client_id parameter when construcing a GIS object. You need this in your Python code to log in.
You can then log on to your org using the Python API using the code shown below:

Once you have followed the process described at the link above to generate a Client ID in ArcGIS Online, use the following Python code in your script but update the test_client_id string to reflect the ID you obtain by following the directions in the API documentation:

import arcgis
test_client_id = 'PASTE-YOUR-CLIENT-ID-HERE'
gis = arcgis.gis.GIS('', client_id=test_client_id)

After the script runs for a few seconds you should be prompted to log in to ArcGIS Online in your web browser and after logging in and returning to your Python prompt. Once you log in successfully you will be presented with a code which you will need to copy and then you should return to your Python prompt where you should see the following message asking for your input. You will then need to paste in the code you copied just moments earlier. Keep in mind that if you are working on a PC in the standard Python prompt with the Command Prompt application, CTRL-V may not work for pasting in the copied code and you may receive an error about the code being incorrect if you try this approach. Instead just use your mouse to right click in the prompt after you’ve made sure that you’ve copied the authentication code, and then hit Enter on your keyboard – the right click operation should successfully paste in the code and allow the rest of the script to run successfully. If the authentication process is successful the final line of the code provided above should print your user information confirming that the script was able to access your account information without any problems.

Once you have successfully used your script to connect to your UT linked ArcGIS Online account, you can refer back to the full ArcGIS Python API documentation for additional information about the functionality you can leverage in your work.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 Generic License.