Skip to content

Instantly share code, notes, and snippets.

@khibma
Last active December 28, 2015 10:19
Show Gist options
  • Save khibma/7485243 to your computer and use it in GitHub Desktop.
Save khibma/7485243 to your computer and use it in GitHub Desktop.
Make arcgis.com token
def gentoken(username, password, referer, expiration=60):
#Re-usable function to get a token required for Admin changes
referer = "http://www.arcgis.com/"
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']
import sys
sys.exit()
else:
# Return the token to the function which called for it
return token['token']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment