Example usage for Google OpenID::
- class GoogleHandler(tornado.web.RequestHandler, tornado.auth.GoogleMixin):
+ class GoogleLoginHandler(tornado.web.RequestHandler,
+ tornado.auth.GoogleMixin):
@tornado.web.asynchronous
+ @tornado.gen.coroutine
def get(self):
if self.get_argument("openid.mode", None):
- self.get_authenticated_user(self.async_callback(self._on_auth))
- return
- self.authenticate_redirect()
-
- def _on_auth(self, user):
- if not user:
- raise tornado.web.HTTPError(500, "Google auth failed")
- # Save the user with, e.g., set_secure_cookie()
+ user = yield self.get_authenticated_user()
+ # Save the user with e.g. set_secure_cookie()
+ else:
+ self.authenticate_redirect()
"""
from __future__ import absolute_import, division, print_function, with_statement
class TwitterLoginHandler(tornado.web.RequestHandler,
tornado.auth.TwitterMixin):
@tornado.web.asynchronous
+ @tornado.gen.coroutine
def get(self):
if self.get_argument("oauth_token", None):
- self.get_authenticated_user(self.async_callback(self._on_auth))
- return
- self.authorize_redirect()
-
- def _on_auth(self, user):
- if not user:
- raise tornado.web.HTTPError(500, "Twitter auth failed")
- # Save the user using, e.g., set_secure_cookie()
+ user = yield self.get_authenticated_user()
+ # Save the user using e.g. set_secure_cookie()
+ else:
+ self.authorize_redirect()
The user object returned by `get_authenticated_user()` includes the
attributes ``username``, ``name``, ``access_token``, and all of the
tornado.auth.TwitterMixin):
@tornado.web.authenticated
@tornado.web.asynchronous
+ @tornado.gen.coroutine
def get(self):
- self.twitter_request(
+ new_entry = yield self.twitter_request(
"/statuses/update",
post_args={"status": "Testing Tornado Web Server"},
- access_token=user["access_token"],
- callback=self.async_callback(self._on_post))
-
- def _on_post(self, new_entry):
+ access_token=self.current_user["access_token"])
if not new_entry:
# Call failed; perhaps missing permission?
self.authorize_redirect()
When your application is set up, you can use this mixin like this
to authenticate the user with FriendFeed and get access to their feed::
- class FriendFeedHandler(tornado.web.RequestHandler,
- tornado.auth.FriendFeedMixin):
+ class FriendFeedLoginHandler(tornado.web.RequestHandler,
+ tornado.auth.FriendFeedMixin):
@tornado.web.asynchronous
+ @tornado.gen.coroutine
def get(self):
if self.get_argument("oauth_token", None):
- self.get_authenticated_user(self.async_callback(self._on_auth))
- return
- self.authorize_redirect()
-
- def _on_auth(self, user):
- if not user:
- raise tornado.web.HTTPError(500, "FriendFeed auth failed")
- # Save the user using, e.g., set_secure_cookie()
+ user = yield self.get_authenticated_user()
+ # Save the user using e.g. set_secure_cookie()
+ else:
+ self.authorize_redirect()
The user object returned by `~OAuthMixin.get_authenticated_user()` includes the
attributes ``username``, ``name``, and ``description`` in addition to
tornado.auth.FriendFeedMixin):
@tornado.web.authenticated
@tornado.web.asynchronous
+ @tornado.gen.coroutine
def get(self):
- self.friendfeed_request(
+ new_entry = yield self.friendfeed_request(
"/entry",
post_args={"body": "Testing Tornado Web Server"},
- access_token=self.current_user["access_token"],
- callback=self.async_callback(self._on_post))
+ access_token=self.current_user["access_token"])
- def _on_post(self, new_entry):
if not new_entry:
# Call failed; perhaps missing permission?
self.authorize_redirect()
Example usage::
- class GoogleHandler(tornado.web.RequestHandler, tornado.auth.GoogleMixin):
+ class GoogleLoginHandler(tornado.web.RequestHandler,
+ tornado.auth.GoogleMixin):
@tornado.web.asynchronous
+ @tornado.gen.coroutine
def get(self):
if self.get_argument("openid.mode", None):
- self.get_authenticated_user(self.async_callback(self._on_auth))
- return
- self.authenticate_redirect()
-
- def _on_auth(self, user):
- if not user:
- raise tornado.web.HTTPError(500, "Google auth failed")
- # Save the user with, e.g., set_secure_cookie()
-
+ user = yield self.get_authenticated_user()
+ # Save the user with e.g. set_secure_cookie()
+ else:
+ self.authenticate_redirect()
"""
_OPENID_ENDPOINT = "https://www.google.com/accounts/o8/ud"
_OAUTH_ACCESS_TOKEN_URL = "https://www.google.com/accounts/OAuthGetAccessToken"
class FacebookGraphLoginHandler(LoginHandler, tornado.auth.FacebookGraphMixin):
@tornado.web.asynchronous
+ @tornado.gen.coroutine
def get(self):
if self.get_argument("code", False):
- self.get_authenticated_user(
- redirect_uri='/auth/facebookgraph/',
- client_id=self.settings["facebook_api_key"],
- client_secret=self.settings["facebook_secret"],
- code=self.get_argument("code"),
- callback=self.async_callback(
- self._on_login))
- return
- self.authorize_redirect(redirect_uri='/auth/facebookgraph/',
- client_id=self.settings["facebook_api_key"],
- extra_params={"scope": "read_stream,offline_access"})
-
- def _on_login(self, user):
- logging.error(user)
- self.finish()
-
+ user = yield self.get_authenticated_user(
+ redirect_uri='/auth/facebookgraph/',
+ client_id=self.settings["facebook_api_key"],
+ client_secret=self.settings["facebook_secret"],
+ code=self.get_argument("code"))
+ # Save the user with e.g. set_secure_cookie
+ else:
+ self.authorize_redirect(
+ redirect_uri='/auth/facebookgraph/',
+ client_id=self.settings["facebook_api_key"],
+ extra_params={"scope": "read_stream,offline_access"})
"""
http = self.get_auth_http_client()
args = {
tornado.auth.FacebookGraphMixin):
@tornado.web.authenticated
@tornado.web.asynchronous
+ @tornado.gen.coroutine
def get(self):
- self.facebook_request(
+ new_entry = yield self.facebook_request(
"/me/feed",
post_args={"message": "I am posting from my Tornado application!"},
- access_token=self.current_user["access_token"],
- callback=self.async_callback(self._on_post))
+ access_token=self.current_user["access_token"])
- def _on_post(self, new_entry):
if not new_entry:
# Call failed; perhaps missing permission?
self.authorize_redirect()
return
self.finish("Posted a message!")
-
"""
url = "https://graph.facebook.com" + path
all_args = {}