Created
June 1, 2016 13:55
-
-
Save kirang89/fbf7546d3161d6505360f5eb76b4dbe6 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
import flask | |
import uuid | |
import httplib2 | |
from oauth2client.client import flow_from_clientsecrets | |
from oauth2client.client import OAuth2Credentials, AccessTokenRefreshError | |
from apiclient.discovery import build | |
app = flask.Flask(__name__) | |
app.secret_key = str(uuid.uuid4()) | |
@app.route('/') | |
def index(): | |
if 'credentials' not in flask.session: | |
return flask.redirect(flask.url_for('oauth_callback')) | |
credentials = OAuth2Credentials.from_json(flask.session['credentials']) | |
if credentials.access_token_expired: | |
return flask.redirect(flask.url_for('oauth_callback')) | |
else: | |
http_auth = credentials.authorize(httplib2.Http()) | |
service = build('calendar', 'v3', http=http_auth) | |
try: | |
request = service.events().list(calendarId='primary') | |
while request is not None: | |
response = request.execute() | |
for event in response.get('items', []): | |
print(repr(event.get('summary', 'NO SUMMARY')) + '\n') | |
request = service.events().list_next(request, response) | |
except AccessTokenRefreshError: | |
print ('The credentials have been revoked or expired, please re-run' | |
'the application to re-authorize') | |
return 'Hello, World!' | |
@app.route('/c') | |
def oauth_callback(): | |
flow = flow_from_clientsecrets('client_id.json', | |
scope='https://www.googleapis.com/auth/calendar', | |
redirect_uri='http://fff9acdd.ngrok.io/c') | |
if 'code' not in flask.request.args: | |
auth_uri = flow.step1_get_authorize_url() | |
return flask.redirect(auth_uri) | |
else: | |
auth_code = flask.request.args.get('code') | |
credentials = flow.step2_exchange(auth_code) | |
flask.session['credentials'] = credentials.to_json() | |
return flask.redirect(flask.url_for('index')) | |
return 'Success' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment