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