Skip to content

Instantly share code, notes, and snippets.

@tcyrus
Created February 20, 2016 16:50
Show Gist options
  • Save tcyrus/68b5607dc319e25ba07e to your computer and use it in GitHub Desktop.
Save tcyrus/68b5607dc319e25ba07e to your computer and use it in GitHub Desktop.
"""
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