Created
April 14, 2016 23:54
-
-
Save rdegges/65c63ad9e17357d78c81d30848f994ab to your computer and use it in GitHub Desktop.
Stormpath Password Grant Authentication Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Stormpath OAuth2 Password Grant Authentication Example | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
This example script shows you how to use the Stormpath Python library to | |
authenticate a user via email and password, getting back an access and | |
refresh token. | |
""" | |
from os import environ | |
from stormpath.api_auth import PasswordGrantAuthenticator | |
from stormpath.client import Client | |
STORMPATH_CLIENT_APIKEY_ID = environ['STORMPATH_CLIENT_APIKEY_ID'] | |
STORMPATH_CLIENT_APIKEY_SECRET = environ['STORMPATH_CLIENT_APIKEY_SECRET'] | |
STORMPATH_APPLICATION_NAME = environ['STORMPATH_APPLICATION_NAME'] | |
STORMPATH_USER_EMAIL = environ['STORMPATH_USER_EMAIL'] | |
STORMPATH_USER_PASSWORD = environ['STORMPATH_USER_PASSWORD'] | |
client = Client(id=STORMPATH_CLIENT_APIKEY_ID, secret=STORMPATH_CLIENT_APIKEY_SECRET) | |
application = client.applications.query(name=STORMPATH_APPLICATION_NAME)[0] | |
authenticator = PasswordGrantAuthenticator(app=application) | |
result = authenticator.authenticate(STORMPATH_USER_EMAIL, STORMPATH_USER_PASSWORD) | |
if result: | |
print('Access Token: {}'.format(result.access_token)) | |
print('Refresh Token: {}'.format(result.refresh_token)) | |
else: | |
print('Invalid credentials supplied.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment