
Public Member Functions | |
| def | authenticate_redirect |
| def | twitter_request |
Private Member Functions | |
| def | _oauth_consumer_token |
| def | _oauth_get_user |
| def | _on_twitter_request |
| def | _parse_user_response |
Static Private Attributes | |
| string | _OAUTH_ACCESS_TOKEN_URL = "http://api.twitter.com/oauth/access_token" |
| string | _OAUTH_AUTHENTICATE_URL = "http://api.twitter.com/oauth/authenticate" |
| string | _OAUTH_AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize" |
| _OAUTH_NO_CALLBACKS = False | |
| string | _OAUTH_REQUEST_TOKEN_URL = "http://api.twitter.com/oauth/request_token" |
Twitter OAuth authentication.
To authenticate with Twitter, register your application with
Twitter at http://twitter.com/apps. Then copy your Consumer Key and
Consumer Secret to the application settings 'twitter_consumer_key' and
'twitter_consumer_secret'. Use this Mixin on the handler for the URL
you registered as your application's Callback URL.
When your application is set up, you can use this Mixin like this
to authenticate the user with Twitter and get access to their stream::
class TwitterHandler(tornado.web.RequestHandler,
tornado.auth.TwitterMixin):
@tornado.web.asynchronous
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()
The user object returned by get_authenticated_user() includes the
attributes 'username', 'name', and all of the custom Twitter user
attributes describe at
http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-users%C2%A0show
in addition to 'access_token'. You should save the access token with
the user; it is required to make requests on behalf of the user later
with twitter_request().
| def tornado.auth.TwitterMixin._oauth_consumer_token | ( | self | ) | [private] |
| def tornado.auth.TwitterMixin._oauth_get_user | ( | self, | |
| access_token, | |||
| callback | |||
| ) | [private] |
Reimplemented from tornado.auth.OAuthMixin.
| def tornado.auth.TwitterMixin._on_twitter_request | ( | self, | |
| callback, | |||
| response | |||
| ) | [private] |
| def tornado.auth.TwitterMixin._parse_user_response | ( | self, | |
| callback, | |||
| user | |||
| ) | [private] |
| def tornado.auth.TwitterMixin.authenticate_redirect | ( | self, | |
callback_uri = None |
|||
| ) |
| def tornado.auth.TwitterMixin.twitter_request | ( | self, | |
| path, | |||
| callback, | |||
access_token = None, |
|||
post_args = None, |
|||
| args | |||
| ) |
Fetches the given API path, e.g., "/statuses/user_timeline/btaylor"
The path should not include the format (we automatically append
".json" and parse the JSON output).
If the request is a POST, post_args should be provided. Query
string arguments should be given as keyword arguments.
All the Twitter methods are documented at
http://apiwiki.twitter.com/Twitter-API-Documentation.
Many methods require an OAuth access token which you can obtain
through authorize_redirect() and get_authenticated_user(). The
user returned through that process includes an 'access_token'
attribute that can be used to make authenticated requests via
this method. Example usage::
class MainHandler(tornado.web.RequestHandler,
tornado.auth.TwitterMixin):
@tornado.web.authenticated
@tornado.web.asynchronous
def get(self):
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):
if not new_entry:
# Call failed; perhaps missing permission?
self.authorize_redirect()
return
self.finish("Posted a message!")
string tornado::auth.TwitterMixin::_OAUTH_ACCESS_TOKEN_URL = "http://api.twitter.com/oauth/access_token" [static, private] |
string tornado::auth.TwitterMixin::_OAUTH_AUTHENTICATE_URL = "http://api.twitter.com/oauth/authenticate" [static, private] |
string tornado::auth.TwitterMixin::_OAUTH_AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize" [static, private] |
tornado::auth.TwitterMixin::_OAUTH_NO_CALLBACKS = False [static, private] |
string tornado::auth.TwitterMixin::_OAUTH_REQUEST_TOKEN_URL = "http://api.twitter.com/oauth/request_token" [static, private] |