socialauth_registered.connect(new_users_handler, sender=None)
+
+Tokens
+------
+
+Almost every service covered provide some kind of API that is protected with
+``access_token`` or token pairs (like `Twitter OAuth keys`_). These tokens are
+gathered by the authentication mechanism and stored in
+``UserSocialAuth.extra_data``.
+
+``UserSocialAuth`` has a property named ``tokens`` to easilly access this
+useful values, it will return a dictionary containing the tokens values.
+A simple usage example::
+
+ >>> from pprint import pprint
+ >>> from social_auth.models import UserSocialAuth
+ >>> instance = UserSocialAuth.objects.filter(provider='twitter').get(...)
+ >>> pprint(instance.tokens)
+ {u'oauth_token': u'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
+ u'oauth_token_secret': u'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'}
+ >>> instance = UserSocialAuth.objects.filter(provider='facebook').get(...)
+ >>> pprint(instance.tokens)
+ {u'access_token': u'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'}
+
+
Backends
--------
.. _BrowserID: https://browserid.org
.. _Instagram API: http://instagr.am/developer/
.. _django-social-auth discussion list: https://groups.google.com/group/django-social-auth
+.. _Twitter OAuth keys: https://dev.twitter.com/docs/auth/authorizing-request
deprecated
signals
+ tokens
contributions
testing
use_cases
--- /dev/null
+Tokens
+------
+
+Almost every service covered provide some kind of API that is protected with
+``access_token`` or token pairs (like `Twitter OAuth keys`_). These tokens are
+gathered by the authentication mechanism and stored in
+``UserSocialAuth.extra_data``.
+
+``UserSocialAuth`` has a property named ``tokens`` to easilly access this
+useful values, it will return a dictionary containing the tokens values.
+A simple usage example::
+
+ >>> from pprint import pprint
+ >>> from social_auth.models import UserSocialAuth
+ >>> instance = UserSocialAuth.objects.filter(provider='twitter').get(...)
+ >>> pprint(instance.tokens)
+ {u'oauth_token': u'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
+ u'oauth_token_secret': u'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'}
+ >>> instance = UserSocialAuth.objects.filter(provider='facebook').get(...)
+ >>> pprint(instance.tokens)
+ {u'access_token': u'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'}
+
+.. _Twitter OAuth keys: https://dev.twitter.com/docs/auth/authorizing-request
"""
raise NotImplementedError('Implement in subclass')
+ @classmethod
+ def tokens(cls, instance):
+ """Return the tokens needed to authenticate the access to any API the
+ service might provide. The return value will be a dictionary with the
+ token type name as key and the token value.
+
+ instance must be a UserSocialAuth instance.
+ """
+ if instance.extra_data and 'access_token' in instance.extra_data:
+ return {
+ 'access_token': instance.extra_data['access_token']
+ }
+ else:
+ return {}
+
def get_user(self, user_id):
"""
Return user with given ID from the User model used by this backend
'first_name': first_name,
'last_name': last_name}
+ @classmethod
+ def tokens(cls, instance):
+ """Return the tokens needed to authenticate the access to any API the
+ service might provide. Twitter uses a pair of OAuthToken consisting on
+ a oauth_token and oauth_token_secret.
+
+ instance must be a UserSocialAuth instance.
+ """
+ token = super(TwitterBackend, cls).tokens(instance)
+ if token and 'access_token' in token:
+ token = dict(tok.split('=')
+ for tok in token['access_token'].split('&'))
+ return token
+
class TwitterAuth(ConsumerBasedOAuth):
"""Twitter OAuth authentication mechanism"""
"""Return associated user unicode representation"""
return unicode(self.user)
+ @property
+ def tokens(self):
+ """Return access_token stored in extra_data or None"""
+ # Make import here to avoid recursive imports :-/
+ from social_auth.backends import get_backends
+ backend = get_backends().get(self.provider)
+ if backend:
+ return backend.AUTH_BACKEND.tokens(self)
+ else:
+ return {}
+
def expiration_delta(self):
"""Return saved session expiration seconds if any. Is retuned in
the form of a timedelta data type. None is returned if there's no