Last active
July 22, 2024 19:35
-
-
Save oevans/6093745 to your computer and use it in GitHub Desktop.
Examples: making standard calls to the ArcGIS Online/Portal REST API via python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
NOTES: | |
- See the ArcGIS REST API documentation for supported operations, methods, and syntax: | |
http://resources.arcgis.com/en/help/arcgis-rest-api/ | |
- A TOKEN must be passed as parameter in addition to any required inputs for the operation. | |
- "urllib.urlencode" handles spaces and other special characters in parameters so that valid URLs are constructed. | |
- "urllib.urlopen" sends the reqest and handles the response. | |
- Parameters are appended to URL for GET (see Example 1), but passed to "urlopen" for POST (see Example 2). API docs specifiy supported methods for each operation. | |
- "json.loads" converts string responses to parseable JSON objects. | |
- Labels in << >> are placeholders and would be replaced with objects in your code. | |
- <<portal>> = 'https://arcgis.com' for ArcGIS Online organizations, or URL to your Portal for ArcGIS web adaptor | |
''' | |
import urllib, json | |
# replace <<PLACEHOLDERS>> in next three lines with your information | |
# e.g., portal = 'https://www.arcgis.com', username = 'jdoe1234', password = 'mypassword' | |
portal = '<<PORTAL>>' | |
username = '<<USERNAME>>' | |
password = '<<PASSWORD>>' | |
# Generate Token Example | |
parameters = urllib.urlencode({'username':username,'password':password,'client':'requestip','f':'json'}) | |
request = portal + '/sharing/rest/generateToken?' | |
response = json.loads(urllib.urlopen(request, parameters).read()) | |
token = response['token'] | |
# EXAMPLE1: request user content (GET method) | |
parameters1 = urllib.urlencode({'token': token, 'f': 'json'}) | |
request1 = portal + '/sharing/rest/content/users/' + username + '?' + parameters # params appended to URL for GET | |
response1 = json.loads(urllib.urlopen(request).read()) | |
# EXAMPLE2: create folder in My Content (requires POST method) | |
parameters2 = urllib.urlencode({'title' : <<title>>, 'token': token, 'f': 'json'}) | |
request2 = portal + '/sharing/rest/content/users/' + username + '/createFolder?' | |
response2 = json.loads(urllib.urlopen(request2, parameters2).read()) # params passed to urlopen for POST |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment