blob: d2a3223a7f46c69ef1180f302e6b85b3a753a39b [file] [log] [blame]
Joe Gregorio845a5452010-09-08 13:50:34 -04001#!/usr/bin/env python
2#
Craig Citro751b7fb2014-09-23 11:20:38 -07003# Copyright 2014 Google Inc. All Rights Reserved.
Joe Gregorio845a5452010-09-08 13:50:34 -04004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Joe Gregorio7b70f432011-11-09 10:18:51 -050017"""Starting template for Google App Engine applications.
18
19Use this project as a starting point if you are just beginning to build a Google
20App Engine project. Remember to download the OAuth 2.0 client secrets which can
21be obtained from the Developer Console <https://code.google.com/apis/console/>
22and save them as 'client_secrets.json' in the project directory.
23"""
Joe Gregorio845a5452010-09-08 13:50:34 -040024
25__author__ = 'jcgregorio@google.com (Joe Gregorio)'
26
27
28import httplib2
Joe Gregoriob8262fc2010-09-09 11:31:30 -040029import logging
Joe Gregorio845a5452010-09-08 13:50:34 -040030import os
Joe Gregorio6abf8702011-03-18 22:44:17 -040031import pickle
Joe Gregorio845a5452010-09-08 13:50:34 -040032
John Asmuth864311d2014-04-24 15:46:08 -040033from googleapiclient import discovery
Joe Gregorio5f8e4fc2013-05-16 15:56:26 -040034from oauth2client import client
Adrian Carpenter2122d3c2016-02-16 16:12:22 -060035from oauth2client.contrib import appengine
Joe Gregorio6abf8702011-03-18 22:44:17 -040036from google.appengine.api import memcache
Joe Gregorio845a5452010-09-08 13:50:34 -040037
Joe Gregorio5f8e4fc2013-05-16 15:56:26 -040038import webapp2
39import jinja2
40
41
42JINJA_ENVIRONMENT = jinja2.Environment(
43 loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
44 autoescape=True,
45 extensions=['jinja2.ext.autoescape'])
Joe Gregorio845a5452010-09-08 13:50:34 -040046
Joe Gregorio7b70f432011-11-09 10:18:51 -050047# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
48# application, including client_id and client_secret, which are found
49# on the API Access tab on the Google APIs
50# Console <http://code.google.com/apis/console>
51CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
52
53# Helpful message to display in the browser if the CLIENT_SECRETS file
54# is missing.
55MISSING_CLIENT_SECRETS_MESSAGE = """
56<h1>Warning: Please configure OAuth 2.0</h1>
57<p>
58To make this sample run you will need to populate the client_secrets.json file
59found at:
60</p>
61<p>
62<code>%s</code>.
63</p>
64<p>with information found on the <a
65href="https://code.google.com/apis/console">APIs Console</a>.
66</p>
67""" % CLIENT_SECRETS
Joe Gregorioc47fab72011-07-06 08:29:20 -040068
69
Joe Gregorio7b70f432011-11-09 10:18:51 -050070http = httplib2.Http(memcache)
Joe Gregorio5f8e4fc2013-05-16 15:56:26 -040071service = discovery.build("plus", "v1", http=http)
72decorator = appengine.oauth2decorator_from_clientsecrets(
Joe Gregorio7b70f432011-11-09 10:18:51 -050073 CLIENT_SECRETS,
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040074 scope='https://www.googleapis.com/auth/plus.me',
75 message=MISSING_CLIENT_SECRETS_MESSAGE)
Joe Gregorio845a5452010-09-08 13:50:34 -040076
Joe Gregorio5f8e4fc2013-05-16 15:56:26 -040077class MainHandler(webapp2.RequestHandler):
Joe Gregorio845a5452010-09-08 13:50:34 -040078
Joe Gregorio7b70f432011-11-09 10:18:51 -050079 @decorator.oauth_aware
Joe Gregorio845a5452010-09-08 13:50:34 -040080 def get(self):
Joe Gregorio7b70f432011-11-09 10:18:51 -050081 variables = {
82 'url': decorator.authorize_url(),
83 'has_credentials': decorator.has_credentials()
84 }
Joe Gregorio5f8e4fc2013-05-16 15:56:26 -040085 template = JINJA_ENVIRONMENT.get_template('grant.html')
86 self.response.write(template.render(variables))
Joe Gregorio845a5452010-09-08 13:50:34 -040087
Joe Gregorio7b70f432011-11-09 10:18:51 -050088
Joe Gregorio5f8e4fc2013-05-16 15:56:26 -040089class AboutHandler(webapp2.RequestHandler):
Joe Gregorio7b70f432011-11-09 10:18:51 -050090
91 @decorator.oauth_required
92 def get(self):
93 try:
94 http = decorator.http()
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040095 user = service.people().get(userId='me').execute(http=http)
Joe Gregorio7b70f432011-11-09 10:18:51 -050096 text = 'Hello, %s!' % user['displayName']
97
Joe Gregorio5f8e4fc2013-05-16 15:56:26 -040098 template = JINJA_ENVIRONMENT.get_template('welcome.html')
99 self.response.write(template.render({'text': text }))
100 except client.AccessTokenRefreshError:
Joe Gregorio7b70f432011-11-09 10:18:51 -0500101 self.redirect('/')
Joe Gregorio845a5452010-09-08 13:50:34 -0400102
103
Joe Gregorio5f8e4fc2013-05-16 15:56:26 -0400104app = webapp2.WSGIApplication(
105 [
106 ('/', MainHandler),
107 ('/about', AboutHandler),
108 (decorator.callback_path, decorator.callback_handler()),
109 ],
110 debug=True)