Created
February 20, 2016 16:50
-
-
Save tcyrus/68b5607dc319e25ba07e to your computer and use it in GitHub Desktop.
This file contains 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
""" | |
An example of Ion's new OAuth | |
For more information see http://tjcsl.github.io/ion/developing/oauth | |
""" | |
import os | |
import json | |
from requests_oauthlib import OAuth2Session | |
CLIENT_ID = 'XXX' | |
CLIENT_SECRET = 'XXX' | |
REDIRECT_URI = 'XXX' | |
AUTHORIZATION_ENDPOINT = 'https://ion.tjhsst.edu/oauth/authorize/' | |
TOKEN_ENDPOINT = 'https://ion.tjhsst.edu/oauth/token/' | |
# If running locally (without HTTPS), override the SSL requirement for OAuth2. | |
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' | |
# Create an OAuth2Session, with the CLIENT_ID and REDIRECT_URI you entered in the application form. | |
# Redirect the user to authorization_url. | |
oauth = OAuth2Session(CLIENT_ID, | |
redirect_uri = REDIRECT_URI, | |
scope = ["read", "write"]) | |
authorization_url, state = oauth.authorization_url("https://ion.tjhsst.edu/oauth/authorize/") | |
# The user authenticates, approves the request, and is redirected to the callback URL specified in redirect_uri, with a “code” GET parameter. | |
token = oauth.fetch_token("https://ion.tjhsst.edu/oauth/token/", | |
code = CODE, | |
client_secret = CLIENT_SECRET) | |
try: | |
profile = oauth.get("http://127.0.0.1/api/profile") | |
except TokenExpiredError as e: | |
args = { "client_id": CLIENT_ID, "client_secret": CLIENT_SECRET } | |
token = oauth.refresh_token("https://ion.tjhsst.edu/oauth/token/", **args) | |
print(json.loads(profile.content.decode())) | |
def refresh(): | |
args = { "client_id": CLIENT_ID, "client_secret": CLIENT_SECRET } | |
token = oauth.refresh_token("https://ion.tjhsst.edu/oauth/token/", **args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment