Generates new google signin users in database and generates json for todays events

- imports caltojson from calendarwatch_server
- gets the user info from google api
- gets user information and compares them to the user database
- returns to login page and shows user information as well as 'api test' button

- api test button uses 'calendarwatch_server' to get calendars and calculate todays events
This commit is contained in:
Raphael Maenle 2020-04-03 10:18:16 +00:00
parent 76c711a58b
commit f86f15069a
2 changed files with 13 additions and 15 deletions

4
.gitignore vendored
View File

@ -1,7 +1,7 @@
# General stuff
.swp
.cache
calendarevents.json
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
@ -144,4 +144,4 @@ cython_debug/
# static files generated from Django application using `collectstatic`
media
static
static

View File

@ -20,6 +20,7 @@ import requests
from db import init_db_command
from user import User
import caltojson
import google.oauth2.credentials
import google_auth_oauthlib.flow
@ -40,10 +41,6 @@ GOOGLE_CLIENT_SECRET = "Hu_YWmKsVKUcLwyeINYzdKfZ"
GOOGLE_DISCOVERY_URL = (
"https://accounts.google.com/.well-known/openid-configuration"
)
API_SERVICE_NAME = 'calendar'
API_VERSION = 'v3'
# Flask app setup
app = Flask(__name__)
app.secret_key = os.environ.get("SECRET_KEY") or os.urandom(24)
@ -95,8 +92,7 @@ def test_api_request():
credentials = google.oauth2.credentials.Credentials(
**flask.session['credentials'])
service = googleapiclient.discovery.build(
API_SERVICE_NAME, API_VERSION, credentials=credentials)
todaysCal = caltojson.generateJsonFromCalendarEntries(credentials)
# Save credentials back to session in case access token was refreshed.
@ -104,7 +100,7 @@ def test_api_request():
# credentials in a persistent database instead.
flask.session['credentials'] = credentials_to_dict(credentials)
return flask.jsonify("{}")
return flask.jsonify(todaysCal)
@app.route("/login")
def login():
@ -151,8 +147,6 @@ def login():
@app.route("/login/callback")
def callback():
print("in callback")
# Specify the state when creating the flow in the callback so that it can
# verified in the authorization server response.
state = flask.session['state']
@ -172,17 +166,21 @@ def callback():
flask.session['credentials'] = credentials_to_dict(credentials)
session = flow.authorized_session()
print(session.get('https://www.googleapis.com/userinfo/v2/me').json())
userinfo = session.get('https://www.googleapis.com/userinfo/v2/me').json()
# Create a user in your db with the information provided
# by Google
user = User(
id_=unique_id, name=users_name, email=users_email, profile_pic=picture
id_=userinfo['id'],
name=userinfo['name'],
email=userinfo['email'],
profile_pic=userinfo['picture']
)
# Doesn't exist? Add it to the database.
if not User.get(unique_id):
User.create(unique_id, users_name, users_email, picture)
if not User.get(user.id):
User.create(user.id, user.name, user.email, user.profile_pic)
# Begin user session by logging the user in
login_user(user)