blob: 087a1366a7686e6be4058ed4734b7a9fc5c559af [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 Gregorio845a5452010-09-08 13:50:34 -040039class Credentials(db.Model):
Joe Gregorio6abf8702011-03-18 22:44:17 -040040 credentials = CredentialsProperty()
Joe Gregorio845a5452010-09-08 13:50:34 -040041
42
43class MainHandler(webapp.RequestHandler):
44
45 @login_required
46 def get(self):
47 user = users.get_current_user()
Joe Gregorio6abf8702011-03-18 22:44:17 -040048 credentials = StorageByKeyName(
49 Credentials, user.user_id(), 'credentials').get()
Joe Gregorio845a5452010-09-08 13:50:34 -040050
Joe Gregorio6abf8702011-03-18 22:44:17 -040051 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 Gregorio845a5452010-09-08 13:50:34 -040068 http = httplib2.Http()
Joe Gregorio6abf8702011-03-18 22:44:17 -040069 http = credentials.authorize(http)
Joe Gregorio1ae3e742011-02-25 15:17:14 -050070 service = build("buzz", "v1", http=http)
71 activities = service.activities()
Joe Gregorio5f087cf2010-09-20 16:08:07 -040072 activitylist = activities.list(scope='@consumption',
73 userId='@me').execute()
Joe Gregorio845a5452010-09-08 13:50:34 -040074 path = os.path.join(os.path.dirname(__file__), 'welcome.html')
Joe Gregoriob8262fc2010-09-09 11:31:30 -040075 logout = users.create_logout_url('/')
76 self.response.out.write(
77 template.render(
78 path, {'activitylist': activitylist,
79 'logout': logout
80 }))
Joe Gregorio845a5452010-09-08 13:50:34 -040081
82
83class OAuthHandler(webapp.RequestHandler):
84
85 @login_required
86 def get(self):
87 user = users.get_current_user()
Joe Gregorio6abf8702011-03-18 22:44:17 -040088 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 Gregorio845a5452010-09-08 13:50:34 -040093 self.redirect("/")
94 else:
95 pass
96
97
98def main():
99 application = webapp.WSGIApplication(
100 [
101 ('/', MainHandler),
102 ('/auth_return', OAuthHandler)
103 ],
104 debug=True)
105 util.run_wsgi_app(application)
106
107
108if __name__ == '__main__':
109 main()