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 | |
| 33 | from apiclient.discovery import build |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 34 | from oauth2client.appengine import oauth2decorator_from_clientsecrets |
| 35 | from oauth2client.client import AccessTokenRefreshError |
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 | from google.appengine.ext import webapp |
| 38 | from google.appengine.ext.webapp import template |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 39 | from google.appengine.ext.webapp.util import run_wsgi_app |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 40 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 41 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 42 | # CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this |
| 43 | # application, including client_id and client_secret, which are found |
| 44 | # on the API Access tab on the Google APIs |
| 45 | # Console <http://code.google.com/apis/console> |
| 46 | CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json') |
| 47 | |
| 48 | # Helpful message to display in the browser if the CLIENT_SECRETS file |
| 49 | # is missing. |
| 50 | MISSING_CLIENT_SECRETS_MESSAGE = """ |
| 51 | <h1>Warning: Please configure OAuth 2.0</h1> |
| 52 | <p> |
| 53 | To make this sample run you will need to populate the client_secrets.json file |
| 54 | found at: |
| 55 | </p> |
| 56 | <p> |
| 57 | <code>%s</code>. |
| 58 | </p> |
| 59 | <p>with information found on the <a |
| 60 | href="https://code.google.com/apis/console">APIs Console</a>. |
| 61 | </p> |
| 62 | """ % CLIENT_SECRETS |
Joe Gregorio | c47fab7 | 2011-07-06 08:29:20 -0400 | [diff] [blame] | 63 | |
| 64 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 65 | http = httplib2.Http(memcache) |
| 66 | service = build("plus", "v1", http=http) |
| 67 | decorator = oauth2decorator_from_clientsecrets( |
| 68 | CLIENT_SECRETS, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 69 | scope='https://www.googleapis.com/auth/plus.me', |
| 70 | message=MISSING_CLIENT_SECRETS_MESSAGE) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 71 | |
| 72 | class MainHandler(webapp.RequestHandler): |
| 73 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 74 | @decorator.oauth_aware |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 75 | def get(self): |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 76 | path = os.path.join(os.path.dirname(__file__), 'grant.html') |
| 77 | variables = { |
| 78 | 'url': decorator.authorize_url(), |
| 79 | 'has_credentials': decorator.has_credentials() |
| 80 | } |
| 81 | self.response.out.write(template.render(path, variables)) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 82 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 83 | |
| 84 | class AboutHandler(webapp.RequestHandler): |
| 85 | |
| 86 | @decorator.oauth_required |
| 87 | def get(self): |
| 88 | try: |
| 89 | http = decorator.http() |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 90 | user = service.people().get(userId='me').execute(http=http) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 91 | text = 'Hello, %s!' % user['displayName'] |
| 92 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 93 | path = os.path.join(os.path.dirname(__file__), 'welcome.html') |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 94 | self.response.out.write(template.render(path, {'text': text })) |
| 95 | except AccessTokenRefreshError: |
| 96 | self.redirect('/') |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 97 | |
| 98 | |
| 99 | def main(): |
| 100 | application = webapp.WSGIApplication( |
| 101 | [ |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 102 | ('/', MainHandler), |
| 103 | ('/about', AboutHandler), |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 104 | (decorator.callback_path, decorator.callback_handler()), |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 105 | ], |
| 106 | debug=True) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 107 | run_wsgi_app(application) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 108 | |
| 109 | |
| 110 | if __name__ == '__main__': |
| 111 | main() |