From 7889cb0f3bec26458623f29dccac5d6734e21316 Mon Sep 17 00:00:00 2001 From: rushilwiz Date: Fri, 15 May 2020 19:22:59 -0400 Subject: [PATCH] working --- chirper/users/templates/users/callback.html | 6 ----- chirper/users/urls.py | 1 - chirper/users/views.py | 25 +++++++++------------ 3 files changed, 11 insertions(+), 21 deletions(-) delete mode 100644 chirper/users/templates/users/callback.html diff --git a/chirper/users/templates/users/callback.html b/chirper/users/templates/users/callback.html deleted file mode 100644 index 5875086..0000000 --- a/chirper/users/templates/users/callback.html +++ /dev/null @@ -1,6 +0,0 @@ -{% extends "blog/base.html" %} -{% block content %} -
- Sign in with Ion -
-{% endblock content %} diff --git a/chirper/users/urls.py b/chirper/users/urls.py index 69559dc..8f9e766 100644 --- a/chirper/users/urls.py +++ b/chirper/users/urls.py @@ -3,5 +3,4 @@ from . import views urlpatterns = [ path('', views.login, name='login'), - path('callback/', views.callback), ] diff --git a/chirper/users/views.py b/chirper/users/views.py index 4ba5de7..f4a20b4 100644 --- a/chirper/users/views.py +++ b/chirper/users/views.py @@ -1,33 +1,30 @@ from django.shortcuts import render, redirect from requests_oauthlib import OAuth2Session +from oauthlib.oauth2 import TokenExpiredError from django.views.decorators.csrf import csrf_exempt +import json import requests # Create your views here. client_id = r'6p7HJlFCD8cnBNBEdgMsdULS5ph0jserw1xvWfxX' client_secret = r'E1e79KebxzAp0LBEtxcUg32b0qFP9Ap9Dxqkac6Qhci5AwXFhSfrbe7MtmGJUh6DDgxivJpGgFYNQgusfvoSraDAnsq3NnEET5DmxgfBBvvuYc2bwDq6KpeKIDQqFtwz' -redirect_uri = 'http://localhost:8000/login/callback/' +redirect_uri = 'http://localhost:8000/login/' token_url = 'https://ion.tjhsst.edu/oauth/authorize/' scope=["read","write"] def login(request): - client = requests.session() + if request.method == "GET": + code = request.GET.get('code') + if code is not None: + payload = {'grant_type':'authorization_code','code': code,'redirect_uri':redirect_uri,'client_id':client_id,'client_secret':client_secret, 'csrfmiddlewaretoken': request.GET.get("state")} + token = requests.post("https://ion.tjhsst.edu/oauth/token/", data=payload) + # profile = requests.post("https://ion.tjhsst.edu/api/profile", headers="Authorization: Bearer {token.access_token}") + return redirect('/') + oauth = OAuth2Session(client_id=client_id, redirect_uri=redirect_uri, scope=scope) authorization_url, state = oauth.authorization_url("https://ion.tjhsst.edu/oauth/authorize/") print(f'a_url: {authorization_url}') return redirect(authorization_url) - -# {'grant_type':'authorization_code','code':code,'redirect_uri':redirect_uri,'client_id':client_id,'client_secret':client_secret} - - -def callback(request): - return render(request, 'users/callback.html') - code = fr"{request.GET.get('code')}" - payload = {'grant_type':'authorization_code','code':code,'redirect_uri':redirect_uri,'client_id':client_id,'client_secret':client_secret} - token = requests.post(token_url, data=payload) - #print (code) - #token = oauth.fetch_token(token_url=token_url, code=code, client_secret=client_secret) - print(token)