Skip to content

Instantly share code, notes, and snippets.

@simong
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save simong/8821437 to your computer and use it in GitHub Desktop.

Select an option

Save simong/8821437 to your computer and use it in GitHub Desktop.
OAE OAuth Python
from rauth import OAuth2Service
import json
# Setup the OAE service
oae = OAuth2Service(
client_id='cPGS0OS0UtIMM8RFAQq596WbJ8oSXfe1',
client_secret='oAxG0hWqzDafjHmROTVEWdhj8qEpGZMR',
name='Test',
authorize_url='http://cam.oae.com/oauth/authorize',
access_token_url='http://cam.oae.com/api/auth/oauth/v2/token',
base_url='http://cam.oae.com')
# There's no need to do a proper OAuth authorization cycle
# We can use the much easier client credentials
data = dict(grant_type = 'client_credentials')
# If you're able to store the access token somewhere, you can retrieve it with the following one-off snippet
# token = oae.get_access_token('POST', decoder=json.loads, data=data)
#
# <Store token>
#
# Use the token to get an authenticated session, since you've stored the token you can re-use it later
# session = oae.get_session(token=token)
#
# Or if you're not planning on storing the token, you can just retrieve a new one for each request (not really recommended)
session = oae.get_auth_session('POST', decoder=json.loads, data=data)
# In case you want to create a local account for a user
createParams = {
'username': 'jd123', # The username the user will use for local authentication
'password': 'hunter2', # His or her password
'displayName': 'John Doe', # The displayName for this user. This is used in the UI
# Optional parameters
'email': 'john.doe@example.com',
'locale': 'en_US', # The user his locale. Should be of the form <language>_<territory> where territory is provided in upper case
'timezone': 'EST', # The timezone this user lives in
'visibility': 'public', # One of 'public', 'loggedin' or 'private'
'publicAlias': 'jd123', # An alias in case this users want's to be private, or the tenant default visibility is set to private
'acceptedTC': 'false' # Whether or not they've accepted the terms and conditions, irrelevant if you don't have any
}
r = session.post('/api/user/create', data=createParams)
print r.content
# In case you want to create users that have a CAS authentication strategy (for example)
files = {'file': ('file', 'jd123,Doe,John,john.doe@example.com\n')}
r = session.post('/api/user/import', data={'authenticationStrategy': 'cas'}, files=files)
print r.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment