blob: 184f5fa8ed6eecd0d0cb2912ee50d622b15a33a8 [file] [log] [blame]
Joe Gregorio845a5452010-09-08 13:50:34 -04001#!/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
21import httplib2
Joe Gregoriob8262fc2010-09-09 11:31:30 -040022import logging
Joe Gregorio845a5452010-09-08 13:50:34 -040023import os
Joe Gregorio6abf8702011-03-18 22:44:17 -040024import pickle
Joe Gregorio845a5452010-09-08 13:50:34 -040025
26from apiclient.discovery import build
Joe Gregorio6abf8702011-03-18 22:44:17 -040027from oauth2client.appengine import CredentialsProperty
28from oauth2client.appengine import StorageByKeyName
29from oauth2client.client import OAuth2WebServerFlow
30from google.appengine.api import memcache
Joe Gregorio845a5452010-09-08 13:50:34 -040031from google.appengine.api import users
32from google.appengine.ext import db
33from google.appengine.ext import webapp
34from google.appengine.ext.webapp import template
35from google.appengine.ext.webapp import util
36from google.appengine.ext.webapp.util import login_required
37
Joe Gregorio845a5452010-09-08 13:50:34 -040038
Joe Gregorioc47fab72011-07-06 08:29:20 -040039FLOW = 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 Gregorio845a5452010-09-08 13:50:34 -040049class Credentials(db.Model):
Joe Gregorio6abf8702011-03-18 22:44:17 -040050 credentials = CredentialsProperty()
Joe Gregorio845a5452010-09-08 13:50:34 -040051
52
53class MainHandler(webapp.RequestHandler):
54
55 @login_required
56 def get(self):
57 user = users.get_current_user()
Joe Gregorio6abf8702011-03-18 22:44:17 -040058 credentials = StorageByKeyName(
59 Credentials, user.user_id(), 'credentials').get()
Joe Gregorio845a5452010-09-08 13:50:34 -040060
Joe Gregorio6abf8702011-03-18 22:44:17 -040061 if credentials is None or credentials.invalid == True:
Joe Gregorioc47fab72011-07-06 08:29:20 -040062 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 Gregorio6abf8702011-03-18 22:44:17 -040065 self.redirect(authorize_url)
66 else:
Joe Gregorio845a5452010-09-08 13:50:34 -040067 http = httplib2.Http()
Joe Gregorio6abf8702011-03-18 22:44:17 -040068 http = credentials.authorize(http)
Joe Gregorio1ae3e742011-02-25 15:17:14 -050069 service = build("buzz", "v1", http=http)
70 activities = service.activities()
Joe Gregorio5f087cf2010-09-20 16:08:07 -040071 activitylist = activities.list(scope='@consumption',
72 userId='@me').execute()
Joe Gregorio845a5452010-09-08 13:50:34 -040073 path = os.path.join(os.path.dirname(__file__), 'welcome.html')
Joe Gregoriob8262fc2010-09-09 11:31:30 -040074 logout = users.create_logout_url('/')
75 self.response.out.write(
76 template.render(
77 path, {'activitylist': activitylist,
78 'logout': logout
79 }))
Joe Gregorio845a5452010-09-08 13:50:34 -040080
81
82class OAuthHandler(webapp.RequestHandler):
83
84 @login_required
85 def get(self):
86 user = users.get_current_user()
Joe Gregorio6abf8702011-03-18 22:44:17 -040087 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 Gregorio845a5452010-09-08 13:50:34 -040092 self.redirect("/")
93 else:
94 pass
95
96
97def main():
98 application = webapp.WSGIApplication(
99 [
100 ('/', MainHandler),
Joe Gregorioc47fab72011-07-06 08:29:20 -0400101 ('/oauth2callback', OAuthHandler)
Joe Gregorio845a5452010-09-08 13:50:34 -0400102 ],
103 debug=True)
104 util.run_wsgi_app(application)
105
106
107if __name__ == '__main__':
108 main()