blob: f649bfba2b0c4ea78c39a2f51303df226e11416f [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#
Joe Gregorio7b70f432011-11-09 10:18:51 -050017"""Starting template for Google App Engine applications.
18
19Use this project as a starting point if you are just beginning to build a Google
20App Engine project. Remember to download the OAuth 2.0 client secrets which can
21be obtained from the Developer Console <https://code.google.com/apis/console/>
22and save them as 'client_secrets.json' in the project directory.
23"""
Joe Gregorio845a5452010-09-08 13:50:34 -040024
25__author__ = 'jcgregorio@google.com (Joe Gregorio)'
26
27
28import httplib2
Joe Gregoriob8262fc2010-09-09 11:31:30 -040029import logging
Joe Gregorio845a5452010-09-08 13:50:34 -040030import os
Joe Gregorio6abf8702011-03-18 22:44:17 -040031import pickle
Joe Gregorio845a5452010-09-08 13:50:34 -040032
33from apiclient.discovery import build
Joe Gregorio7b70f432011-11-09 10:18:51 -050034from oauth2client.appengine import oauth2decorator_from_clientsecrets
35from oauth2client.client import AccessTokenRefreshError
Joe Gregorio6abf8702011-03-18 22:44:17 -040036from google.appengine.api import memcache
Joe Gregorio845a5452010-09-08 13:50:34 -040037from google.appengine.ext import webapp
38from google.appengine.ext.webapp import template
Joe Gregorio7b70f432011-11-09 10:18:51 -050039from google.appengine.ext.webapp.util import run_wsgi_app
Joe Gregorio845a5452010-09-08 13:50:34 -040040
Joe Gregorio845a5452010-09-08 13:50:34 -040041
Joe Gregorio7b70f432011-11-09 10:18:51 -050042# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
43# application, including client_id and client_secret, which are found
44# on the API Access tab on the Google APIs
45# Console <http://code.google.com/apis/console>
46CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
47
48# Helpful message to display in the browser if the CLIENT_SECRETS file
49# is missing.
50MISSING_CLIENT_SECRETS_MESSAGE = """
51<h1>Warning: Please configure OAuth 2.0</h1>
52<p>
53To make this sample run you will need to populate the client_secrets.json file
54found at:
55</p>
56<p>
57<code>%s</code>.
58</p>
59<p>with information found on the <a
60href="https://code.google.com/apis/console">APIs Console</a>.
61</p>
62""" % CLIENT_SECRETS
Joe Gregorioc47fab72011-07-06 08:29:20 -040063
64
Joe Gregorio7b70f432011-11-09 10:18:51 -050065http = httplib2.Http(memcache)
66service = build("plus", "v1", http=http)
67decorator = oauth2decorator_from_clientsecrets(
68 CLIENT_SECRETS,
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040069 scope='https://www.googleapis.com/auth/plus.me',
70 message=MISSING_CLIENT_SECRETS_MESSAGE)
Joe Gregorio845a5452010-09-08 13:50:34 -040071
72class MainHandler(webapp.RequestHandler):
73
Joe Gregorio7b70f432011-11-09 10:18:51 -050074 @decorator.oauth_aware
Joe Gregorio845a5452010-09-08 13:50:34 -040075 def get(self):
Joe Gregorio7b70f432011-11-09 10:18:51 -050076 path = os.path.join(os.path.dirname(__file__), 'grant.html')
77 variables = {
78 'url': decorator.authorize_url(),
79 'has_credentials': decorator.has_credentials()
80 }
81 self.response.out.write(template.render(path, variables))
Joe Gregorio845a5452010-09-08 13:50:34 -040082
Joe Gregorio7b70f432011-11-09 10:18:51 -050083
84class AboutHandler(webapp.RequestHandler):
85
86 @decorator.oauth_required
87 def get(self):
88 try:
89 http = decorator.http()
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040090 user = service.people().get(userId='me').execute(http=http)
Joe Gregorio7b70f432011-11-09 10:18:51 -050091 text = 'Hello, %s!' % user['displayName']
92
Joe Gregorio845a5452010-09-08 13:50:34 -040093 path = os.path.join(os.path.dirname(__file__), 'welcome.html')
Joe Gregorio7b70f432011-11-09 10:18:51 -050094 self.response.out.write(template.render(path, {'text': text }))
95 except AccessTokenRefreshError:
96 self.redirect('/')
Joe Gregorio845a5452010-09-08 13:50:34 -040097
98
99def main():
100 application = webapp.WSGIApplication(
101 [
Joe Gregorio7b70f432011-11-09 10:18:51 -0500102 ('/', MainHandler),
103 ('/about', AboutHandler),
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400104 (decorator.callback_path, decorator.callback_handler()),
Joe Gregorio845a5452010-09-08 13:50:34 -0400105 ],
106 debug=True)
Joe Gregorio7b70f432011-11-09 10:18:51 -0500107 run_wsgi_app(application)
Joe Gregorio845a5452010-09-08 13:50:34 -0400108
109
110if __name__ == '__main__':
111 main()