• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright 2014 Google Inc. All Rights Reserved.
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"""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"""
24
25__author__ = 'jcgregorio@google.com (Joe Gregorio)'
26
27
28import httplib2
29import logging
30import os
31import pickle
32
33from googleapiclient import discovery
34from oauth2client import client
35from oauth2client.contrib import appengine
36from google.appengine.api import memcache
37
38import webapp2
39import jinja2
40
41
42JINJA_ENVIRONMENT = jinja2.Environment(
43    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
44    autoescape=True,
45    extensions=['jinja2.ext.autoescape'])
46
47# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
48# application, including client_id and client_secret, which are found
49# on the API Access tab on the Google APIs
50# Console <http://code.google.com/apis/console>
51CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
52
53# Helpful message to display in the browser if the CLIENT_SECRETS file
54# is missing.
55MISSING_CLIENT_SECRETS_MESSAGE = """
56<h1>Warning: Please configure OAuth 2.0</h1>
57<p>
58To make this sample run you will need to populate the client_secrets.json file
59found at:
60</p>
61<p>
62<code>%s</code>.
63</p>
64<p>with information found on the <a
65href="https://code.google.com/apis/console">APIs Console</a>.
66</p>
67""" % CLIENT_SECRETS
68
69
70http = httplib2.Http(memcache)
71service = discovery.build("plus", "v1", http=http)
72decorator = appengine.oauth2decorator_from_clientsecrets(
73    CLIENT_SECRETS,
74    scope='https://www.googleapis.com/auth/plus.me',
75    message=MISSING_CLIENT_SECRETS_MESSAGE)
76
77class MainHandler(webapp2.RequestHandler):
78
79  @decorator.oauth_aware
80  def get(self):
81    variables = {
82        'url': decorator.authorize_url(),
83        'has_credentials': decorator.has_credentials()
84        }
85    template = JINJA_ENVIRONMENT.get_template('grant.html')
86    self.response.write(template.render(variables))
87
88
89class AboutHandler(webapp2.RequestHandler):
90
91  @decorator.oauth_required
92  def get(self):
93    try:
94      http = decorator.http()
95      user = service.people().get(userId='me').execute(http=http)
96      text = 'Hello, %s!' % user['displayName']
97
98      template = JINJA_ENVIRONMENT.get_template('welcome.html')
99      self.response.write(template.render({'text': text }))
100    except client.AccessTokenRefreshError:
101      self.redirect('/')
102
103
104app = webapp2.WSGIApplication(
105    [
106     ('/', MainHandler),
107     ('/about', AboutHandler),
108     (decorator.callback_path, decorator.callback_handler()),
109    ],
110    debug=True)
111