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 | # |
| 17 | |
| 18 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 19 | |
| 20 | |
| 21 | import httplib2 |
Joe Gregorio | b8262fc | 2010-09-09 11:31:30 -0400 | [diff] [blame] | 22 | import logging |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 23 | import os |
| 24 | |
| 25 | from apiclient.discovery import build |
| 26 | from apiclient.ext.appengine import FlowThreeLeggedProperty |
| 27 | from apiclient.ext.appengine import OAuthCredentialsProperty |
| 28 | from apiclient.oauth import FlowThreeLegged |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 29 | from google.appengine.api import users |
| 30 | from google.appengine.ext import db |
| 31 | from google.appengine.ext import webapp |
| 32 | from google.appengine.ext.webapp import template |
| 33 | from google.appengine.ext.webapp import util |
| 34 | from google.appengine.ext.webapp.util import login_required |
| 35 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 36 | |
| 37 | class Flow(db.Model): |
| 38 | # FlowThreeLegged could also be stored in memcache. |
| 39 | flow = FlowThreeLeggedProperty() |
| 40 | |
| 41 | |
| 42 | class Credentials(db.Model): |
| 43 | credentials = OAuthCredentialsProperty() |
| 44 | |
| 45 | |
| 46 | class MainHandler(webapp.RequestHandler): |
| 47 | |
| 48 | @login_required |
| 49 | def get(self): |
| 50 | user = users.get_current_user() |
| 51 | c = Credentials.get_by_key_name(user.user_id()) |
| 52 | |
| 53 | if c: |
| 54 | http = httplib2.Http() |
| 55 | http = c.credentials.authorize(http) |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame^] | 56 | service = build("buzz", "v1", http=http) |
| 57 | activities = service.activities() |
Joe Gregorio | 5f087cf | 2010-09-20 16:08:07 -0400 | [diff] [blame] | 58 | activitylist = activities.list(scope='@consumption', |
| 59 | userId='@me').execute() |
Joe Gregorio | b8262fc | 2010-09-09 11:31:30 -0400 | [diff] [blame] | 60 | logging.info(activitylist) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 61 | path = os.path.join(os.path.dirname(__file__), 'welcome.html') |
Joe Gregorio | b8262fc | 2010-09-09 11:31:30 -0400 | [diff] [blame] | 62 | logout = users.create_logout_url('/') |
| 63 | self.response.out.write( |
| 64 | template.render( |
| 65 | path, {'activitylist': activitylist, |
| 66 | 'logout': logout |
| 67 | })) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 68 | else: |
Joe Gregorio | f6a598b | 2010-09-22 15:01:38 -0400 | [diff] [blame] | 69 | p = build("buzz", "v1") |
| 70 | flow = FlowThreeLegged(p.auth_discovery(), |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 71 | consumer_key='anonymous', |
| 72 | consumer_secret='anonymous', |
| 73 | user_agent='google-api-client-python-buzz-webapp/1.0', |
| 74 | domain='anonymous', |
| 75 | scope='https://www.googleapis.com/auth/buzz', |
| 76 | xoauth_displayname='Example Web App') |
| 77 | |
ade@google.com | 39e2a4e | 2011-01-10 00:33:07 +0000 | [diff] [blame] | 78 | callback = self.request.relative_url('/auth_return') |
| 79 | authorize_url = flow.step1_get_authorize_url(callback) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 80 | f = Flow(key_name=user.user_id(), flow=flow) |
| 81 | f.put() |
| 82 | self.redirect(authorize_url) |
| 83 | |
| 84 | |
| 85 | class OAuthHandler(webapp.RequestHandler): |
| 86 | |
| 87 | @login_required |
| 88 | def get(self): |
| 89 | user = users.get_current_user() |
| 90 | f = Flow.get_by_key_name(user.user_id()) |
| 91 | if f: |
| 92 | credentials = f.flow.step2_exchange(self.request.params) |
| 93 | c = Credentials(key_name=user.user_id(), credentials=credentials) |
| 94 | c.put() |
| 95 | f.delete() |
| 96 | self.redirect("/") |
| 97 | else: |
| 98 | pass |
| 99 | |
| 100 | |
| 101 | def main(): |
| 102 | application = webapp.WSGIApplication( |
| 103 | [ |
| 104 | ('/', MainHandler), |
| 105 | ('/auth_return', OAuthHandler) |
| 106 | ], |
| 107 | debug=True) |
| 108 | util.run_wsgi_app(application) |
| 109 | |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | main() |