Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. |
| 4 | # |
| 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 Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 17 | """Starting template for Google App Engine applications. |
| 18 | |
| 19 | Use this project as a starting point if you are just beginning to build a Google |
| 20 | App Engine project. Remember to download the OAuth 2.0 client secrets which can |
| 21 | be obtained from the Developer Console <https://code.google.com/apis/console/> |
| 22 | and save them as 'client_secrets.json' in the project directory. |
| 23 | """ |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 24 | |
| 25 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 26 | |
| 27 | |
| 28 | import httplib2 |
Joe Gregorio | b8262fc | 2010-09-09 11:31:30 -0400 | [diff] [blame] | 29 | import logging |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 30 | import os |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 31 | import pickle |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 32 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 33 | from googleapiclient import discovery |
Joe Gregorio | 5f8e4fc | 2013-05-16 15:56:26 -0400 | [diff] [blame] | 34 | from oauth2client import appengine |
| 35 | from oauth2client import client |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 36 | from google.appengine.api import memcache |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 37 | |
Joe Gregorio | 5f8e4fc | 2013-05-16 15:56:26 -0400 | [diff] [blame] | 38 | import webapp2 |
| 39 | import jinja2 |
| 40 | |
| 41 | |
| 42 | JINJA_ENVIRONMENT = jinja2.Environment( |
| 43 | loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), |
| 44 | autoescape=True, |
| 45 | extensions=['jinja2.ext.autoescape']) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 46 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 47 | # 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> |
| 51 | CLIENT_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. |
| 55 | MISSING_CLIENT_SECRETS_MESSAGE = """ |
| 56 | <h1>Warning: Please configure OAuth 2.0</h1> |
| 57 | <p> |
| 58 | To make this sample run you will need to populate the client_secrets.json file |
| 59 | found at: |
| 60 | </p> |
| 61 | <p> |
| 62 | <code>%s</code>. |
| 63 | </p> |
| 64 | <p>with information found on the <a |
| 65 | href="https://code.google.com/apis/console">APIs Console</a>. |
| 66 | </p> |
| 67 | """ % CLIENT_SECRETS |
Joe Gregorio | c47fab7 | 2011-07-06 08:29:20 -0400 | [diff] [blame] | 68 | |
| 69 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 70 | http = httplib2.Http(memcache) |
Joe Gregorio | 5f8e4fc | 2013-05-16 15:56:26 -0400 | [diff] [blame] | 71 | service = discovery.build("plus", "v1", http=http) |
| 72 | decorator = appengine.oauth2decorator_from_clientsecrets( |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 73 | CLIENT_SECRETS, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 74 | scope='https://www.googleapis.com/auth/plus.me', |
| 75 | message=MISSING_CLIENT_SECRETS_MESSAGE) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 76 | |
Joe Gregorio | 5f8e4fc | 2013-05-16 15:56:26 -0400 | [diff] [blame] | 77 | class MainHandler(webapp2.RequestHandler): |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 78 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 79 | @decorator.oauth_aware |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 80 | def get(self): |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 81 | variables = { |
| 82 | 'url': decorator.authorize_url(), |
| 83 | 'has_credentials': decorator.has_credentials() |
| 84 | } |
Joe Gregorio | 5f8e4fc | 2013-05-16 15:56:26 -0400 | [diff] [blame] | 85 | template = JINJA_ENVIRONMENT.get_template('grant.html') |
| 86 | self.response.write(template.render(variables)) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 87 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 88 | |
Joe Gregorio | 5f8e4fc | 2013-05-16 15:56:26 -0400 | [diff] [blame] | 89 | class AboutHandler(webapp2.RequestHandler): |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 90 | |
| 91 | @decorator.oauth_required |
| 92 | def get(self): |
| 93 | try: |
| 94 | http = decorator.http() |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 95 | user = service.people().get(userId='me').execute(http=http) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 96 | text = 'Hello, %s!' % user['displayName'] |
| 97 | |
Joe Gregorio | 5f8e4fc | 2013-05-16 15:56:26 -0400 | [diff] [blame] | 98 | template = JINJA_ENVIRONMENT.get_template('welcome.html') |
| 99 | self.response.write(template.render({'text': text })) |
| 100 | except client.AccessTokenRefreshError: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 101 | self.redirect('/') |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 102 | |
| 103 | |
Joe Gregorio | 5f8e4fc | 2013-05-16 15:56:26 -0400 | [diff] [blame] | 104 | app = webapp2.WSGIApplication( |
| 105 | [ |
| 106 | ('/', MainHandler), |
| 107 | ('/about', AboutHandler), |
| 108 | (decorator.callback_path, decorator.callback_handler()), |
| 109 | ], |
| 110 | debug=True) |