|
| 1 | +#!/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 | + |
| 21 | +import httplib2 |
| 22 | +import logging |
| 23 | +import os |
| 24 | +import pickle |
| 25 | + |
| 26 | +from apiclient.discovery import build |
| 27 | +from oauth2client.appengine import CredentialsProperty |
| 28 | +from oauth2client.appengine import StorageByKeyName |
| 29 | +from oauth2client.client import OAuth2WebServerFlow |
| 30 | +from google.appengine.api import memcache |
| 31 | +from google.appengine.api import users |
| 32 | +from google.appengine.ext import db |
| 33 | +from google.appengine.ext import webapp |
| 34 | +from google.appengine.ext.webapp import template |
| 35 | +from google.appengine.ext.webapp import util |
| 36 | +from google.appengine.ext.webapp.util import login_required |
| 37 | + |
| 38 | + |
| 39 | +class Credentials(db.Model): |
| 40 | + credentials = CredentialsProperty() |
| 41 | + |
| 42 | + |
| 43 | +class MainHandler(webapp.RequestHandler): |
| 44 | + |
| 45 | + @login_required |
| 46 | + def get(self): |
| 47 | + user = users.get_current_user() |
| 48 | + credentials = StorageByKeyName( |
| 49 | + Credentials, user.user_id(), 'credentials').get() |
| 50 | + |
| 51 | + if credentials is None or credentials.invalid == True: |
| 52 | + flow = OAuth2WebServerFlow( |
| 53 | + client_id='2ad565600216d25d9cde', |
| 54 | + client_secret='03b56df2949a520be6049ff98b89813f17b467dc', |
| 55 | + scope='read', |
| 56 | + user_agent='oauth2client-sample/1.0', |
| 57 | + auth_uri='https://api.dailymotion.com/oauth/authorize', |
| 58 | + token_uri='https://api.dailymotion.com/oauth/token' |
| 59 | + ) |
| 60 | + |
| 61 | + callback = self.request.relative_url('/auth_return') |
| 62 | + authorize_url = flow.step1_get_authorize_url(callback) |
| 63 | + memcache.set(user.user_id(), pickle.dumps(flow)) |
| 64 | + self.redirect(authorize_url) |
| 65 | + else: |
| 66 | + http = httplib2.Http() |
| 67 | + |
| 68 | + resp, content1 = http.request('https://api.dailymotion.com/me?access_token=%s' % |
| 69 | + credentials.access_token) |
| 70 | + |
| 71 | + http = credentials.authorize(http) |
| 72 | + resp, content2 = http.request('https://api.dailymotion.com/me') |
| 73 | + |
| 74 | + path = os.path.join(os.path.dirname(__file__), 'welcome.html') |
| 75 | + logout = users.create_logout_url('/') |
| 76 | + self.response.out.write( |
| 77 | + template.render( |
| 78 | + path, { |
| 79 | + 'content1': content1, |
| 80 | + 'content2': content2, |
| 81 | + 'logout': logout |
| 82 | + })) |
| 83 | + |
| 84 | + |
| 85 | +class OAuthHandler(webapp.RequestHandler): |
| 86 | + |
| 87 | + @login_required |
| 88 | + def get(self): |
| 89 | + user = users.get_current_user() |
| 90 | + flow = pickle.loads(memcache.get(user.user_id())) |
| 91 | + if flow: |
| 92 | + credentials = flow.step2_exchange(self.request.params) |
| 93 | + StorageByKeyName( |
| 94 | + Credentials, user.user_id(), 'credentials').put(credentials) |
| 95 | + self.redirect("/") |
| 96 | + else: |
| 97 | + pass |
| 98 | + |
| 99 | + |
| 100 | +def main(): |
| 101 | + application = webapp.WSGIApplication( |
| 102 | + [ |
| 103 | + ('/', MainHandler), |
| 104 | + ('/auth_return', OAuthHandler) |
| 105 | + ], |
| 106 | + debug=True) |
| 107 | + util.run_wsgi_app(application) |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + main() |
0 commit comments