blob: 9f63d504f283a779516e0a3be2d2efd4f711930f [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
Joe Gregorio845a5452010-09-08 13:50:34 -040029from google.appengine.api import users
30from google.appengine.ext import db
31from google.appengine.ext import webapp
32from google.appengine.ext.webapp import template
33from google.appengine.ext.webapp import util
34from google.appengine.ext.webapp.util import login_required
35
Joe Gregoriob8262fc2010-09-09 11:31:30 -040036STEP2_URI = 'http://%s.appspot.com/auth_return' % os.environ['APPLICATION_ID']
Joe Gregorio845a5452010-09-08 13:50:34 -040037
38
39class Flow(db.Model):
40 # FlowThreeLegged could also be stored in memcache.
41 flow = FlowThreeLeggedProperty()
42
43
44class Credentials(db.Model):
45 credentials = OAuthCredentialsProperty()
46
47
48class 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()
Joe Gregorio5f087cf2010-09-20 16:08:07 -040060 activitylist = activities.list(scope='@consumption',
61 userId='@me').execute()
Joe Gregoriob8262fc2010-09-09 11:31:30 -040062 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:
Joe Gregoriof6a598b2010-09-22 15:01:38 -040071 p = build("buzz", "v1")
72 flow = FlowThreeLegged(p.auth_discovery(),
Joe Gregorio845a5452010-09-08 13:50:34 -040073 consumer_key='anonymous',
74 consumer_secret='anonymous',
75 user_agent='google-api-client-python-buzz-webapp/1.0',
76 domain='anonymous',
77 scope='https://www.googleapis.com/auth/buzz',
78 xoauth_displayname='Example Web App')
79
80 authorize_url = flow.step1_get_authorize_url(STEP2_URI)
81 f = Flow(key_name=user.user_id(), flow=flow)
82 f.put()
83 self.redirect(authorize_url)
84
85
86class OAuthHandler(webapp.RequestHandler):
87
88 @login_required
89 def get(self):
90 user = users.get_current_user()
91 f = Flow.get_by_key_name(user.user_id())
92 if f:
93 credentials = f.flow.step2_exchange(self.request.params)
94 c = Credentials(key_name=user.user_id(), credentials=credentials)
95 c.put()
96 f.delete()
97 self.redirect("/")
98 else:
99 pass
100
101
102def main():
103 application = webapp.WSGIApplication(
104 [
105 ('/', MainHandler),
106 ('/auth_return', OAuthHandler)
107 ],
108 debug=True)
109 util.run_wsgi_app(application)
110
111
112if __name__ == '__main__':
113 main()