Skip to content

Instantly share code, notes, and snippets.

@stephenquan
Created December 9, 2021 05:50
Show Gist options
  • Save stephenquan/662a6369f0272166359a6db184b4d4e0 to your computer and use it in GitHub Desktop.
Save stephenquan/662a6369f0272166359a6db184b4d4e0 to your computer and use it in GitHub Desktop.
oauth2login.py
import json, urllib.parse, urllib.request
CLIENT_ID = 'appstudioweb'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
# Show OAuth2 dialog and get OAuth2 secret
print('Open a web browser and navigate to:')
print('')
print('https://www.arcgis.com/sharing/rest/oauth2/authorize/?hidecancel=true&client_id=appstudioweb&grant_type=code&response_type=code&expiration=-1&redirect_uri=urn:ietf:wg:oauth:2.0:oob&locale=en')
print('')
secret = input('Copy and paste response here: ')
# Obtain refresh token
target = 'https://www.arcgis.com/sharing/rest/oauth2/token'
parameters = { }
parameters['grant_type'] = 'authorization_code'
parameters['client_id'] = CLIENT_ID
parameters['code'] = secret
parameters['redirect_uri'] = REDIRECT_URI
data = urllib.parse.urlencode(parameters)
data = data.encode('ascii')
response = { }
with urllib.request.urlopen(target, data) as f:
response = json.load(f)
if 'error' in response:
print(response.get('error'))
raise Exception(response.get('error').get('message'))
refresh_token = response.get('refresh_token')
print('')
print('Refresh Token: ' + refresh_token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment