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