blob: 14c6593c425a787f15f13b670a973bc8406f6475 [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
24
25from apiclient.discovery import build
26from apiclient.ext.appengine import FlowThreeLeggedProperty
27from apiclient.ext.appengine import OAuthCredentialsProperty
28from apiclient.oauth import FlowThreeLegged
29from apiclient.oauth import buzz_discovery
30from google.appengine.api import users
31from google.appengine.ext import db
32from google.appengine.ext import webapp
33from google.appengine.ext.webapp import template
34from google.appengine.ext.webapp import util
35from google.appengine.ext.webapp.util import login_required
36
Joe Gregoriob8262fc2010-09-09 11:31:30 -040037STEP2_URI = 'http://%s.appspot.com/auth_return' % os.environ['APPLICATION_ID']
Joe Gregorio845a5452010-09-08 13:50:34 -040038
39
40class Flow(db.Model):
41 # FlowThreeLegged could also be stored in memcache.
42 flow = FlowThreeLeggedProperty()
43
44
45class Credentials(db.Model):
46 credentials = OAuthCredentialsProperty()
47
48
49class MainHandler(webapp.RequestHandler):
50
51 @login_required
52 def get(self):
53 user = users.get_current_user()
54 c = Credentials.get_by_key_name(user.user_id())
55
56 if c:
57 http = httplib2.Http()
58 http = c.credentials.authorize(http)
59 p = build("buzz", "v1", http=http)
60 activities = p.activities()
Joe Gregoriob8262fc2010-09-09 11:31:30 -040061 activitylist = activities.list(scope='@consumption', userId='@me')
62 logging.info(activitylist)
Joe Gregorio845a5452010-09-08 13:50:34 -040063 path = os.path.join(os.path.dirname(__file__), 'welcome.html')
Joe Gregoriob8262fc2010-09-09 11:31:30 -040064 logout = users.create_logout_url('/')
65 self.response.out.write(
66 template.render(
67 path, {'activitylist': activitylist,
68 'logout': logout
69 }))
Joe Gregorio845a5452010-09-08 13:50:34 -040070 else:
71 flow = FlowThreeLegged(buzz_discovery,
72 consumer_key='anonymous',
73 consumer_secret='anonymous',
74 user_agent='google-api-client-python-buzz-webapp/1.0',
75 domain='anonymous',
76 scope='https://www.googleapis.com/auth/buzz',
77 xoauth_displayname='Example Web App')
78
79 authorize_url = flow.step1_get_authorize_url(STEP2_URI)
80 f = Flow(key_name=user.user_id(), flow=flow)
81 f.put()
82 self.redirect(authorize_url)
83
84
85class 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
101def main():
102 application = webapp.WSGIApplication(
103 [
104 ('/', MainHandler),
105 ('/auth_return', OAuthHandler)
106 ],
107 debug=True)
108 util.run_wsgi_app(application)
109
110
111if __name__ == '__main__':
112 main()