blob: 78634e7dc6defd8d39ac9a91f7e7c600cccdbe05 [file] [log] [blame]
Joe Gregorio695fdc12011-01-16 16:46:55 -05001#!/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
22import logging
23import os
24import pickle
25
26from apiclient.discovery import build
27from oauth2client.appengine import CredentialsProperty
28from oauth2client.appengine import StorageByKeyName
29from oauth2client.client import OAuth2WebServerFlow
30from google.appengine.api import memcache
31from 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
38
39class Credentials(db.Model):
40 credentials = CredentialsProperty()
41
42
43class MainHandler(webapp.RequestHandler):
44
45 @login_required
46 def get(self):
47 user = users.get_current_user()
Joe Gregoriodeeb0202011-02-15 14:49:57 -050048 credentials = StorageByKeyName(
49 Credentials, user.user_id(), 'credentials').get()
Joe Gregorio695fdc12011-01-16 16:46:55 -050050
51 if credentials:
52 http = httplib2.Http()
53 http = credentials.authorize(http)
54 p = build("buzz", "v1", http=http)
55 activities = p.activities()
56 activitylist = activities.list(scope='@consumption',
57 userId='@me').execute()
58 path = os.path.join(os.path.dirname(__file__), 'welcome.html')
59 logout = users.create_logout_url('/')
60 self.response.out.write(
61 template.render(
62 path, {'activitylist': activitylist,
63 'logout': logout
64 }))
65 else:
66 flow = OAuth2WebServerFlow(
Joe Gregorio49e94d82011-01-28 16:36:13 -050067 # Visit https://code.google.com/apis/console to
68 # generate your client_id, client_secret and to
69 # register your redirect_uri.
70 client_id='<YOUR CLIENT ID HERE>',
71 client_secret='<YOUR CLIENT SECRET HERE>',
Joe Gregorio695fdc12011-01-16 16:46:55 -050072 scope='https://www.googleapis.com/auth/buzz',
73 user_agent='buzz-cmdline-sample/1.0',
74 domain='anonymous',
75 xoauth_displayname='Google App Engine Example App')
76
77 callback = self.request.relative_url('/auth_return')
78 authorize_url = flow.step1_get_authorize_url(callback)
79 memcache.set(user.user_id(), pickle.dumps(flow))
80 self.redirect(authorize_url)
81
82
83class OAuthHandler(webapp.RequestHandler):
84
85 @login_required
86 def get(self):
87 user = users.get_current_user()
88 flow = pickle.loads(memcache.get(user.user_id()))
89 if flow:
90 credentials = flow.step2_exchange(self.request.params)
Joe Gregoriodeeb0202011-02-15 14:49:57 -050091 StorageByKeyName(
92 Credentials, user.user_id(), 'credentials').put(credentials)
Joe Gregorio695fdc12011-01-16 16:46:55 -050093 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()