Added sample for dailymotion
This commit is contained in:
1
samples/oauth2/dailymotion/apiclient
Symbolic link
1
samples/oauth2/dailymotion/apiclient
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../apiclient/
|
||||
9
samples/oauth2/dailymotion/app.yaml
Normal file
9
samples/oauth2/dailymotion/app.yaml
Normal file
@@ -0,0 +1,9 @@
|
||||
application: dailymotoauth2test
|
||||
version: 1
|
||||
runtime: python
|
||||
api_version: 1
|
||||
|
||||
handlers:
|
||||
- url: .*
|
||||
script: main.py
|
||||
|
||||
1
samples/oauth2/dailymotion/gflags.py
Symbolic link
1
samples/oauth2/dailymotion/gflags.py
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../gflags.py
|
||||
1
samples/oauth2/dailymotion/gflags_validators.py
Symbolic link
1
samples/oauth2/dailymotion/gflags_validators.py
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../gflags_validators.py
|
||||
1
samples/oauth2/dailymotion/httplib2
Symbolic link
1
samples/oauth2/dailymotion/httplib2
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../httplib2/
|
||||
11
samples/oauth2/dailymotion/index.yaml
Normal file
11
samples/oauth2/dailymotion/index.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
indexes:
|
||||
|
||||
# AUTOGENERATED
|
||||
|
||||
# This index.yaml is automatically updated whenever the dev_appserver
|
||||
# detects that a new type of query is run. If you want to manage the
|
||||
# index.yaml file manually, remove the above marker line (the line
|
||||
# saying "# AUTOGENERATED"). If you want to manage some indexes
|
||||
# manually, move them above the marker line. The index.yaml file is
|
||||
# automatically uploaded to the admin console when you next deploy
|
||||
# your application using appcfg.py.
|
||||
111
samples/oauth2/dailymotion/main.py
Normal file
111
samples/oauth2/dailymotion/main.py
Normal file
@@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright 2007 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
|
||||
|
||||
|
||||
import httplib2
|
||||
import logging
|
||||
import os
|
||||
import pickle
|
||||
|
||||
from apiclient.discovery import build
|
||||
from oauth2client.appengine import CredentialsProperty
|
||||
from oauth2client.appengine import StorageByKeyName
|
||||
from oauth2client.client import OAuth2WebServerFlow
|
||||
from google.appengine.api import memcache
|
||||
from google.appengine.api import users
|
||||
from google.appengine.ext import db
|
||||
from google.appengine.ext import webapp
|
||||
from google.appengine.ext.webapp import template
|
||||
from google.appengine.ext.webapp import util
|
||||
from google.appengine.ext.webapp.util import login_required
|
||||
|
||||
|
||||
class Credentials(db.Model):
|
||||
credentials = CredentialsProperty()
|
||||
|
||||
|
||||
class MainHandler(webapp.RequestHandler):
|
||||
|
||||
@login_required
|
||||
def get(self):
|
||||
user = users.get_current_user()
|
||||
credentials = StorageByKeyName(
|
||||
Credentials, user.user_id(), 'credentials').get()
|
||||
|
||||
if credentials is None or credentials.invalid == True:
|
||||
flow = OAuth2WebServerFlow(
|
||||
client_id='2ad565600216d25d9cde',
|
||||
client_secret='03b56df2949a520be6049ff98b89813f17b467dc',
|
||||
scope='read',
|
||||
user_agent='oauth2client-sample/1.0',
|
||||
auth_uri='https://api.dailymotion.com/oauth/authorize',
|
||||
token_uri='https://api.dailymotion.com/oauth/token'
|
||||
)
|
||||
|
||||
callback = self.request.relative_url('/auth_return')
|
||||
authorize_url = flow.step1_get_authorize_url(callback)
|
||||
memcache.set(user.user_id(), pickle.dumps(flow))
|
||||
self.redirect(authorize_url)
|
||||
else:
|
||||
http = httplib2.Http()
|
||||
|
||||
resp, content1 = http.request('https://api.dailymotion.com/me?access_token=%s' %
|
||||
credentials.access_token)
|
||||
|
||||
http = credentials.authorize(http)
|
||||
resp, content2 = http.request('https://api.dailymotion.com/me')
|
||||
|
||||
path = os.path.join(os.path.dirname(__file__), 'welcome.html')
|
||||
logout = users.create_logout_url('/')
|
||||
self.response.out.write(
|
||||
template.render(
|
||||
path, {
|
||||
'content1': content1,
|
||||
'content2': content2,
|
||||
'logout': logout
|
||||
}))
|
||||
|
||||
|
||||
class OAuthHandler(webapp.RequestHandler):
|
||||
|
||||
@login_required
|
||||
def get(self):
|
||||
user = users.get_current_user()
|
||||
flow = pickle.loads(memcache.get(user.user_id()))
|
||||
if flow:
|
||||
credentials = flow.step2_exchange(self.request.params)
|
||||
StorageByKeyName(
|
||||
Credentials, user.user_id(), 'credentials').put(credentials)
|
||||
self.redirect("/")
|
||||
else:
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
application = webapp.WSGIApplication(
|
||||
[
|
||||
('/', MainHandler),
|
||||
('/auth_return', OAuthHandler)
|
||||
],
|
||||
debug=True)
|
||||
util.run_wsgi_app(application)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
1
samples/oauth2/dailymotion/oauth2client
Symbolic link
1
samples/oauth2/dailymotion/oauth2client
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../oauth2client/
|
||||
1
samples/oauth2/dailymotion/simplejson
Symbolic link
1
samples/oauth2/dailymotion/simplejson
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../simplejson/
|
||||
1
samples/oauth2/dailymotion/uritemplate
Symbolic link
1
samples/oauth2/dailymotion/uritemplate
Symbolic link
@@ -0,0 +1 @@
|
||||
../../../uritemplate/
|
||||
16
samples/oauth2/dailymotion/welcome.html
Normal file
16
samples/oauth2/dailymotion/welcome.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Daily Motion Sample</title>
|
||||
<style type=text/css>
|
||||
td { vertical-align: top; padding: 0.5em }
|
||||
img { border:0 }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<p><a href="{{ logout }}">Logout</a></p>
|
||||
<h2>First request with access_token in query parameter:</h2>
|
||||
<pre>{{ content1 }} </pre>
|
||||
<h2>Second request with access_token in header:</h2>
|
||||
<pre>{{ content2 }} </pre>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user