Skip to content

Instantly share code, notes, and snippets.

@nohe427
Created August 26, 2015 18:01
Show Gist options
  • Save nohe427/3452fd96bd7ae32270c9 to your computer and use it in GitHub Desktop.
Save nohe427/3452fd96bd7ae32270c9 to your computer and use it in GitHub Desktop.
This gets all users in an ArcGIS Online organization
import urllib
import urllib2
import json
class ArcGISOnline(object):
def __init__(self, Username, Password):
self.username = Username
self.password = Password
self.__token = self.generateToken(self.username, self.password)['token']
self.__protocol = self.__useProtocol()
self.__short = self.__GetInfo()['urlKey']
@staticmethod
def generateToken(username, password):
'''Generate a token using urllib modules for the input
username and password'''
url = "https://arcgis.com/sharing/generateToken"
data = {'username': username,
'password': password,
'referer' : 'https://arcgis.com',
'expires' : 1209600,
'f': 'json'}
data = urllib.urlencode(data)
request = urllib2.Request(url, data)
response = urllib2.urlopen(request)
return json.loads(response.read())
@property
def token(self):
'''Makes the non-public token read-only as a public token property'''
return self.__token
def __useProtocol(self):
tokenResponse = self.generateToken(self.username, self.password)
if tokenResponse['ssl']:
ssl = 'https'
else:
ssl = 'http'
return ssl
def __GetInfo(self):
'''Get information about the specified organization
this information includes the Short name of the organization (['urlKey'])
as well as the organization ID ['id']'''
URL= '{}://arcgis.com/sharing/rest/portals/self?f=json&token={}'.format(self.__protocol,self.__token)
request = urllib2.Request(URL)
response = urllib2.urlopen(request)
return json.loads(response.read())
def FindWebMap(self, webmapName):
'''Returns the details for the item returned that
matches the itemname and has a type of web map.
To access the webmap ID, use the key ['id']'''
ItemsURL = '{}://{}.maps.arcgis.com/sharing/rest/content/users/{}?f=json&token={}'.format(self.__protocol, self.__short, self.username, self.__token)
Request = urllib2.Request(ItemsURL)
Response = urllib2.urlopen(Request)
items = json.loads(Response.read())['items']
for item in items:
if item['title'] == webmapName and item['type'] == 'Web Map':
return item['id']
def UpdateItem(self, itemID, JSON):
'''Updates the input webmap with the new JSON loaded,
uses urllib as a get request'''
url = '{}://{}.maps.arcgis.com/sharing/rest/content/users/{}/items/{}/update?token={}&f=json'.format(self.__protocol, self.__short, self.username, itemID, self.__token)
dumped = json.dumps(JSON)
data = {'async': 'True',
'text': dumped}
data = urllib.urlencode(data)
request = urllib2.Request(url, data)
response = urllib2.urlopen(request)
return json.loads(response.read())
def getUsers(self):
'''Gets all users in the ArcGIS Online Organization and returns
them as a list to the user.'''
url = '{}://{}.maps.arcgis.com/sharing/rest/portals/self/users'.format(self.__protocol, self.__short)
data = {}
data["f"] = "pjson"
data["start"] = "1"
data["sortOrder"] = "asc";
data["num"] = "100";
data["sortField"] = "fullname";
data["token"] = self.__token;
users = []
while data["start"] != -1:
dataEncoded = urllib.urlencode(data)
request = urllib2.Request(url, dataEncoded)
response = urllib2.urlopen(request)
response = json.loads(response.read())
for i in response["users"]:
users.append(i.copy())
print response["nextStart"]
data["start"] = response["nextStart"]
return users
if __name__ == '__main__':
username = raw_input("Please enter your username: ")
password = raw_input("Please enter your password: ")
onlineAccount = ArcGISOnline(username, password)
allUsers = onlineAccount.getUsers()
for user in allUsers:
print user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment