1 # Copyright (C) 2008 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 __author__
= 'api.jscudder (Jeff Scudder)'
19 import wsgiref
.handlers
20 from google
.appengine
.ext
import webapp
21 from google
.appengine
.api
import users
24 import gdata
.alt
.appengine
28 class Fetcher(webapp
.RequestHandler
):
31 # Write our pages title
32 self
.response
.out
.write("""<html><head><title>
33 Google Data Feed Fetcher: read Google Data API Atom feeds</title>""")
34 self
.response
.out
.write('</head><body>')
35 # Allow the user to sign in or sign out
36 next_url
= atom
.url
.Url('http', settings
.HOST_NAME
, path
='/step2')
37 if users
.get_current_user():
38 self
.response
.out
.write('<a href="%s">Sign Out</a><br>' % (
39 users
.create_logout_url(str(next_url
))))
41 self
.response
.out
.write('<a href="%s">Sign In</a><br>' % (
42 users
.create_login_url(str(next_url
))))
44 # Initialize a client to talk to Google Data API services.
45 client
= gdata
.service
.GDataService()
46 gdata
.alt
.appengine
.run_on_appengine(client
)
49 # Find the AuthSub token and upgrade it to a session token.
50 auth_token
= gdata
.auth
.extract_auth_sub_token_from_url(self
.request
.uri
)
52 # Upgrade the single-use AuthSub token to a multi-use session token.
53 session_token
= client
.upgrade_to_session_token(auth_token
)
54 if session_token
and users
.get_current_user():
55 # If there is a current user, store the token in the datastore and
56 # associate it with the current user. Since we told the client to
57 # run_on_appengine, the add_token call will automatically store the
58 # session token if there is a current_user.
59 client
.token_store
.add_token(session_token
)
61 # Since there is no current user, we will put the session token
62 # in a property of the client. We will not store the token in the
63 # datastore, since we wouldn't know which user it belongs to.
64 # Since a new client object is created with each get call, we don't
65 # need to worry about the anonymous token being used by other users.
66 client
.current_token
= session_token
68 self
.response
.out
.write('<div id="main"></div>')
69 self
.response
.out
.write(
70 '<div id="sidebar"><div id="scopes"><h4>Request a token</h4><ul>')
71 self
.response
.out
.write('<li><a href="%s">Google Documents</a></li>' % (
72 client
.GenerateAuthSubURL(
74 ('http://docs.google.com/feeds/',), secure
=False, session
=True)))
75 self
.response
.out
.write('</ul></div><br/><div id="tokens">')
79 application
= webapp
.WSGIApplication([('/.*', Fetcher
),], debug
=True)
80 wsgiref
.handlers
.CGIHandler().run(application
)
83 if __name__
== '__main__':