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 |
| 22 | import os |
| 23 | |
| 24 | from apiclient.discovery import build |
| 25 | from apiclient.ext.appengine import FlowThreeLeggedProperty |
| 26 | from apiclient.ext.appengine import OAuthCredentialsProperty |
| 27 | from apiclient.oauth import FlowThreeLegged |
| 28 | from apiclient.oauth import buzz_discovery |
| 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 | |
| 36 | STEP2_URI = 'http://m-buzz.appspot.com/auth_return' |
| 37 | |
| 38 | |
| 39 | class Flow(db.Model): |
| 40 | # FlowThreeLegged could also be stored in memcache. |
| 41 | flow = FlowThreeLeggedProperty() |
| 42 | |
| 43 | |
| 44 | class Credentials(db.Model): |
| 45 | credentials = OAuthCredentialsProperty() |
| 46 | |
| 47 | |
| 48 | class MainHandler(webapp.RequestHandler): |
| 49 | |
| 50 | @login_required |
| 51 | def get(self): |
| 52 | user = users.get_current_user() |
| 53 | c = Credentials.get_by_key_name(user.user_id()) |
| 54 | |
| 55 | if c: |
| 56 | http = httplib2.Http() |
| 57 | http = c.credentials.authorize(http) |
| 58 | p = build("buzz", "v1", http=http) |
| 59 | activities = p.activities() |
| 60 | activitylist = activities.list(scope='@self', userId='@me') |
| 61 | path = os.path.join(os.path.dirname(__file__), 'welcome.html') |
| 62 | self.response.out.write(template.render(path, activitylist)) |
| 63 | else: |
| 64 | flow = FlowThreeLegged(buzz_discovery, |
| 65 | consumer_key='anonymous', |
| 66 | consumer_secret='anonymous', |
| 67 | user_agent='google-api-client-python-buzz-webapp/1.0', |
| 68 | domain='anonymous', |
| 69 | scope='https://www.googleapis.com/auth/buzz', |
| 70 | xoauth_displayname='Example Web App') |
| 71 | |
| 72 | authorize_url = flow.step1_get_authorize_url(STEP2_URI) |
| 73 | f = Flow(key_name=user.user_id(), flow=flow) |
| 74 | f.put() |
| 75 | self.redirect(authorize_url) |
| 76 | |
| 77 | |
| 78 | class OAuthHandler(webapp.RequestHandler): |
| 79 | |
| 80 | @login_required |
| 81 | def get(self): |
| 82 | user = users.get_current_user() |
| 83 | f = Flow.get_by_key_name(user.user_id()) |
| 84 | if f: |
| 85 | credentials = f.flow.step2_exchange(self.request.params) |
| 86 | c = Credentials(key_name=user.user_id(), credentials=credentials) |
| 87 | c.put() |
| 88 | f.delete() |
| 89 | self.redirect("/") |
| 90 | else: |
| 91 | pass |
| 92 | |
| 93 | |
| 94 | def main(): |
| 95 | application = webapp.WSGIApplication( |
| 96 | [ |
| 97 | ('/', MainHandler), |
| 98 | ('/auth_return', OAuthHandler) |
| 99 | ], |
| 100 | debug=True) |
| 101 | util.run_wsgi_app(application) |
| 102 | |
| 103 | |
| 104 | if __name__ == '__main__': |
| 105 | main() |