blob: 7f7256ffbf83db8c91d57583ba8b59d2ebfa6def [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
Joe Gregoriofa6630c2011-03-11 09:15:45 -050029from google.appengine.api import users
30from google.appengine.ext import db
31from google.appengine.ext import webapp
32from google.appengine.ext.webapp import template
33from google.appengine.ext.webapp import util
34from google.appengine.ext.webapp.util import login_required
35
36
Joe Gregorioc47fab72011-07-06 08:29:20 -040037FLOW = OAuth2WebServerFlow(
38 client_id='2ad565600216d25d9cde',
39 client_secret='03b56df2949a520be6049ff98b89813f17b467dc',
40 scope='read',
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040041 redirect_uri='https://dailymotoauth2test.appspot.com/auth_return',
Joe Gregorioc47fab72011-07-06 08:29:20 -040042 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 Gregorio68a8cfe2012-08-03 16:17:40 -040061 authorize_url = FLOW.step1_get_authorize_url()
Joe Gregoriofa6630c2011-03-11 09:15:45 -050062 self.redirect(authorize_url)
63 else:
64 http = httplib2.Http()
Joe Gregoriofa6630c2011-03-11 09:15:45 -050065 http = credentials.authorize(http)
Joe Gregorioc47fab72011-07-06 08:29:20 -040066
67 resp, content = http.request('https://api.dailymotion.com/me')
Joe Gregoriofa6630c2011-03-11 09:15:45 -050068
69 path = os.path.join(os.path.dirname(__file__), 'welcome.html')
70 logout = users.create_logout_url('/')
Joe Gregorioc47fab72011-07-06 08:29:20 -040071 variables = {
72 'content': content,
73 'logout': logout
74 }
75 self.response.out.write(template.render(path, variables))
Joe Gregoriofa6630c2011-03-11 09:15:45 -050076
77
78class OAuthHandler(webapp.RequestHandler):
79
80 @login_required
81 def get(self):
82 user = users.get_current_user()
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040083 credentials = FLOW.step2_exchange(self.request.params)
84 StorageByKeyName(
85 Credentials, user.user_id(), 'credentials').put(credentials)
86 self.redirect("/")
Joe Gregoriofa6630c2011-03-11 09:15:45 -050087
88
89def main():
90 application = webapp.WSGIApplication(
91 [
92 ('/', MainHandler),
93 ('/auth_return', OAuthHandler)
94 ],
95 debug=True)
96 util.run_wsgi_app(application)
97
98
99if __name__ == '__main__':
100 main()