import json, urllib, urllib2 USERNAME = "____" PASSWORD = "_____" REFER = "http://www.arcgis.com" def urlopen(url, data=None): referer = "http://arcgis.com/arcgis/rest" req = urllib2.Request(url) req.add_header('Referer', referer) if data: response = urllib2.urlopen(req, data) else: response = urllib2.urlopen(req) return response def gentoken(username, password, referer, expiration=60): #Re-usable function to get a token query_dict = {'username': username, 'password': password, 'expiration': str(expiration), 'client': 'referer', 'referer': referer, 'f': 'json'} query_string = urllib.urlencode(query_dict) tokenUrl = "https://www.arcgis.com/sharing/rest/generateToken" tokenResponse = urllib.urlopen(tokenUrl, urllib.urlencode(query_dict)) token = json.loads(tokenResponse.read()) if "token" not in token: print token['messages'] exit() else: # Return the token to the function which called for it return token['token'] token = gentoken(USERNAME, PASSWORD, REFER)
Excellent work! This is just what I needed!
Some modifications I had to make to Treg's code in order to get this to work, since it seems Python 3.x handles some things differently:
I had to import http.client instead of httplib
I had to use 'params = urllib.parse.urlencode(...' instead of 'params = urllib.urlencode(...'
I am using a web adaptor, so I left the PORT null, and just changed the tokenURL
Other than that, this worked! Now I just need to figure out how to push edits/additions back up to the feature service via arcpy...