Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [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 | # |
| 17 | |
| 18 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 19 | |
| 20 | |
| 21 | import httplib2 |
| 22 | import logging |
| 23 | import os |
| 24 | import pickle |
| 25 | |
| 26 | from apiclient.discovery import build |
| 27 | from oauth2client.appengine import CredentialsProperty |
| 28 | from oauth2client.appengine import StorageByKeyName |
| 29 | from oauth2client.client import OAuth2WebServerFlow |
| 30 | from google.appengine.api import memcache |
| 31 | from google.appengine.api import users |
| 32 | from google.appengine.ext import db |
| 33 | from google.appengine.ext import webapp |
| 34 | from google.appengine.ext.webapp import template |
| 35 | from google.appengine.ext.webapp import util |
| 36 | from google.appengine.ext.webapp.util import login_required |
| 37 | |
| 38 | |
| 39 | class Credentials(db.Model): |
| 40 | credentials = CredentialsProperty() |
| 41 | |
| 42 | |
| 43 | class MainHandler(webapp.RequestHandler): |
| 44 | |
| 45 | @login_required |
| 46 | def get(self): |
| 47 | user = users.get_current_user() |
| 48 | credentials = StorageByKeyName(Credentials, user.user_id(), 'credentials').get() |
| 49 | |
| 50 | if credentials: |
| 51 | http = httplib2.Http() |
| 52 | http = credentials.authorize(http) |
| 53 | p = build("buzz", "v1", http=http) |
| 54 | activities = p.activities() |
| 55 | activitylist = activities.list(scope='@consumption', |
| 56 | userId='@me').execute() |
| 57 | path = os.path.join(os.path.dirname(__file__), 'welcome.html') |
| 58 | logout = users.create_logout_url('/') |
| 59 | self.response.out.write( |
| 60 | template.render( |
| 61 | path, {'activitylist': activitylist, |
| 62 | 'logout': logout |
| 63 | })) |
| 64 | else: |
| 65 | flow = OAuth2WebServerFlow( |
| 66 | client_id='anonymous', |
| 67 | client_secret='anonymous', |
| 68 | scope='https://www.googleapis.com/auth/buzz', |
| 69 | user_agent='buzz-cmdline-sample/1.0', |
| 70 | domain='anonymous', |
| 71 | xoauth_displayname='Google App Engine Example App') |
| 72 | |
| 73 | callback = self.request.relative_url('/auth_return') |
| 74 | authorize_url = flow.step1_get_authorize_url(callback) |
| 75 | memcache.set(user.user_id(), pickle.dumps(flow)) |
| 76 | self.redirect(authorize_url) |
| 77 | |
| 78 | |
| 79 | class OAuthHandler(webapp.RequestHandler): |
| 80 | |
| 81 | @login_required |
| 82 | def get(self): |
| 83 | user = users.get_current_user() |
| 84 | flow = pickle.loads(memcache.get(user.user_id())) |
| 85 | if flow: |
| 86 | credentials = flow.step2_exchange(self.request.params) |
| 87 | StorageByKeyName(Credentials, user.user_id(), 'credentials').put(credentials) |
| 88 | self.redirect("/") |
| 89 | else: |
| 90 | pass |
| 91 | |
| 92 | |
| 93 | def main(): |
| 94 | application = webapp.WSGIApplication( |
| 95 | [ |
| 96 | ('/', MainHandler), |
| 97 | ('/auth_return', OAuthHandler) |
| 98 | ], |
| 99 | debug=True) |
| 100 | util.run_wsgi_app(application) |
| 101 | |
| 102 | |
| 103 | if __name__ == '__main__': |
| 104 | main() |