blob: 1b752397ce9a4d5156529d399b6e4c6b63f93c60 [file] [log] [blame]
Joe Gregoriofa6630c2011-03-11 09:15:45 -05001#!/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
21import httplib2
22import logging
23import os
24import pickle
25
Joe Gregoriofa6630c2011-03-11 09:15:45 -050026from oauth2client.appengine import CredentialsProperty
27from oauth2client.appengine import StorageByKeyName
28from oauth2client.client import OAuth2WebServerFlow
29from google.appengine.api import memcache
30from google.appengine.api import users
31from google.appengine.ext import db
32from google.appengine.ext import webapp
33from google.appengine.ext.webapp import template
34from google.appengine.ext.webapp import util
35from google.appengine.ext.webapp.util import login_required
36
37
Joe Gregorioc47fab72011-07-06 08:29:20 -040038FLOW = 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 Gregoriofa6630c2011-03-11 09:15:45 -050048class Credentials(db.Model):
49 credentials = CredentialsProperty()
50
51
52class 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 Gregoriofa6630c2011-03-11 09:15:45 -050061 callback = self.request.relative_url('/auth_return')
Joe Gregorioc47fab72011-07-06 08:29:20 -040062 authorize_url = FLOW.step1_get_authorize_url(callback)
63 memcache.set(user.user_id(), pickle.dumps(FLOW))
Joe Gregoriofa6630c2011-03-11 09:15:45 -050064 self.redirect(authorize_url)
65 else:
66 http = httplib2.Http()
Joe Gregoriofa6630c2011-03-11 09:15:45 -050067 http = credentials.authorize(http)
Joe Gregorioc47fab72011-07-06 08:29:20 -040068
69 resp, content = http.request('https://api.dailymotion.com/me')
Joe Gregoriofa6630c2011-03-11 09:15:45 -050070
71 path = os.path.join(os.path.dirname(__file__), 'welcome.html')
72 logout = users.create_logout_url('/')
Joe Gregorioc47fab72011-07-06 08:29:20 -040073 variables = {
74 'content': content,
75 'logout': logout
76 }
77 self.response.out.write(template.render(path, variables))
Joe Gregoriofa6630c2011-03-11 09:15:45 -050078
79
80class 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
95def main():
96 application = webapp.WSGIApplication(
97 [
98 ('/', MainHandler),
99 ('/auth_return', OAuthHandler)
100 ],
101 debug=True)
102 util.run_wsgi_app(application)
103
104
105if __name__ == '__main__':
106 main()