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 |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 24 | import pickle |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 25 | |
| 26 | from apiclient.discovery import build |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 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 |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 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 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 38 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 39 | class Credentials(db.Model): |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 40 | credentials = CredentialsProperty() |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 41 | |
| 42 | |
| 43 | class MainHandler(webapp.RequestHandler): |
| 44 | |
| 45 | @login_required |
| 46 | def get(self): |
| 47 | user = users.get_current_user() |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 48 | credentials = StorageByKeyName( |
| 49 | Credentials, user.user_id(), 'credentials').get() |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 50 | |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 51 | if credentials is None or credentials.invalid == True: |
| 52 | flow = OAuth2WebServerFlow( |
| 53 | # Visit https://code.google.com/apis/console to |
| 54 | # generate your client_id, client_secret and to |
| 55 | # register your redirect_uri. |
| 56 | client_id='<YOUR CLIENT ID HERE>', |
| 57 | client_secret='<YOUR CLIENT SECRET HERE>', |
| 58 | scope='https://www.googleapis.com/auth/buzz', |
| 59 | user_agent='buzz-cmdline-sample/1.0', |
| 60 | domain='anonymous', |
| 61 | xoauth_displayname='Google App Engine Example App') |
| 62 | |
| 63 | callback = self.request.relative_url('/auth_return') |
| 64 | authorize_url = flow.step1_get_authorize_url(callback) |
| 65 | memcache.set(user.user_id(), pickle.dumps(flow)) |
| 66 | self.redirect(authorize_url) |
| 67 | else: |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 68 | http = httplib2.Http() |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 69 | http = credentials.authorize(http) |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame] | 70 | service = build("buzz", "v1", http=http) |
| 71 | activities = service.activities() |
Joe Gregorio | 5f087cf | 2010-09-20 16:08:07 -0400 | [diff] [blame] | 72 | activitylist = activities.list(scope='@consumption', |
| 73 | userId='@me').execute() |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 74 | path = os.path.join(os.path.dirname(__file__), 'welcome.html') |
Joe Gregorio | b8262fc | 2010-09-09 11:31:30 -0400 | [diff] [blame] | 75 | logout = users.create_logout_url('/') |
| 76 | self.response.out.write( |
| 77 | template.render( |
| 78 | path, {'activitylist': activitylist, |
| 79 | 'logout': logout |
| 80 | })) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 81 | |
| 82 | |
| 83 | class OAuthHandler(webapp.RequestHandler): |
| 84 | |
| 85 | @login_required |
| 86 | def get(self): |
| 87 | user = users.get_current_user() |
Joe Gregorio | 6abf870 | 2011-03-18 22:44:17 -0400 | [diff] [blame] | 88 | flow = pickle.loads(memcache.get(user.user_id())) |
| 89 | if flow: |
| 90 | credentials = flow.step2_exchange(self.request.params) |
| 91 | StorageByKeyName( |
| 92 | Credentials, user.user_id(), 'credentials').put(credentials) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 93 | self.redirect("/") |
| 94 | else: |
| 95 | pass |
| 96 | |
| 97 | |
| 98 | def main(): |
| 99 | application = webapp.WSGIApplication( |
| 100 | [ |
| 101 | ('/', MainHandler), |
| 102 | ('/auth_return', OAuthHandler) |
| 103 | ], |
| 104 | debug=True) |
| 105 | util.run_wsgi_app(application) |
| 106 | |
| 107 | |
| 108 | if __name__ == '__main__': |
| 109 | main() |