forked from Esri/ago-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
39 lines (34 loc) · 1.47 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
import urllib
import json
import getpass
class User:
def __init__(self, username, portal=None, password=None):
if portal == None:
self.portalUrl = 'https://arcgis.com'
else:
self.portalUrl = portal
self.username = username
if password == None:
self.password = getpass.getpass()
else:
self.password = password
self.token = self.__getToken__(self.portalUrl, self.username, self.password)
def __getToken__(self, url, username, password):
'''Retrieves a token to be used with future requests.'''
parameters = urllib.urlencode({'username' : username,
'password' : password,
'client' : 'referer',
'referer': url,
'expiration': 60,
'f' : 'json'})
response = urllib.urlopen(url + '/sharing/rest/generateToken?', parameters).read()
token = json.loads(response)['token']
return token
def __portalId__(self):
'''Gets the ID for the organization/portal.'''
parameters = urllib.urlencode({'token' : self.token,
'f' : 'json'})
response = urllib.urlopen(self.portalUrl + '/sharing/rest/portals/self?' + parameters).read()
portalId = json.loads(response)['id']
return portalId