]> git.parisson.com Git - django-social-auth.git/commitdiff
Add Flickr backend
authorDaniel G. Taylor <dan@programmer-art.org>
Mon, 10 Oct 2011 13:39:55 +0000 (09:39 -0400)
committerDaniel G. Taylor <dan@programmer-art.org>
Mon, 10 Oct 2011 13:39:55 +0000 (09:39 -0400)
README.rst
social_auth/backends/contrib/flickr.py [new file with mode: 0644]

index 895332f11ed41bde4270d65bab8158454871cac1..709293c0004cba18d546f74bb685008b621a4921 100644 (file)
@@ -43,6 +43,7 @@ credentials, some features are:
     * `Foursquare OAuth2`_
     * `GitHub OAuth`_
     * `Dropbox OAuth`_
+    * `Flickr OAuth`_
 
 - Basic user data population and signaling, to allows custom fields values
   from providers response
@@ -116,6 +117,7 @@ Configuration
         'social_auth.backends.contrib.foursquare.FoursquareBackend',
         'social_auth.backends.contrib.github.GithubBackend',
         'social_auth.backends.contrib.dropbox.DropboxBackend',
+        'social_auth.backends.contrib.flickr.FlickrBackend',
         'social_auth.backends.OpenIDBackend',
         'django.contrib.auth.backends.ModelBackend',
     )
@@ -158,6 +160,8 @@ Configuration
     GITHUB_API_SECRET            = ''
     DROPBOX_APP_ID               = ''
     DROPBOX_API_SECRET           = ''
+    FLICKR_APP_ID                = ''
+    FLICKR_API_SECRET            = ''
 
 - Setup login URLs::
 
@@ -577,6 +581,20 @@ Dropbox uses OAuth v1.0 for authentication.
       DROPBOX_APP_ID = ''
       DROPBOX_API_SECRET = ''
 
+
+------
+Flickr
+------
+Flickr uses OAuth v1.0 for authentication.
+
+- Register a new application at the `Flickr App Garden`_, and
+
+- fill ``Key`` and ``Secret`` values in the settings::
+
+      FLICKR_APP_ID = ''
+      FLICKR_API_SECRET = ''
+
+
 -------
 Testing
 -------
@@ -767,3 +785,5 @@ Base work is copyrighted by:
 .. _djangopackages.com: http://djangopackages.com/grids/g/social-auth-backends/
 .. _Dropbox OAuth: https://www.dropbox.com/developers_beta/reference/api
 .. _Dropbox Developers: https://www.dropbox.com/developers/apps
+.. _Flickr OAuth: http://www.flickr.com/services/api/
+.. _Flickr App Garden: http://www.flickr.com/services/apps/create/
diff --git a/social_auth/backends/contrib/flickr.py b/social_auth/backends/contrib/flickr.py
new file mode 100644 (file)
index 0000000..f88a444
--- /dev/null
@@ -0,0 +1,80 @@
+"""
+Flickr OAuth support.
+
+This contribution adds support for Flickr OAuth service. The settings
+FLICKR_APP_ID and FLICKR_API_SECRET must be defined with the values
+given by Dropbox application registration process.
+
+By default account id, username and token expiration time are stored in
+extra_data field, check OAuthBackend class for details on how to extend it.
+"""
+try:
+    from urlparse import parse_qs
+    parse_qs # placate pyflakes
+except ImportError:
+    # fall back for Python 2.5
+    from cgi import parse_qs
+
+from django.conf import settings
+from django.utils import simplejson
+
+from oauth2 import Token
+from social_auth.backends import ConsumerBasedOAuth, OAuthBackend, USERNAME
+
+# Dropbox configuration
+FLICKR_SERVER = 'http://www.flickr.com/services'
+FLICKR_REQUEST_TOKEN_URL = '%s/oauth/request_token' % FLICKR_SERVER
+FLICKR_AUTHORIZATION_URL = '%s/oauth/authorize' % FLICKR_SERVER
+FLICKR_ACCESS_TOKEN_URL = '%s/oauth/access_token' % FLICKR_SERVER
+EXPIRES_NAME = getattr(settings, 'SOCIAL_AUTH_EXPIRATION', 'expires')
+
+
+class FlickrBackend(OAuthBackend):
+    """Dropbox OAuth authentication backend"""
+    name = 'flickr'
+    # Default extra data to store
+    EXTRA_DATA = [('id', 'id'), ('username', 'username'), ('expires', EXPIRES_NAME)]
+
+    def get_user_details(self, response):
+        """Return user details from Flickr account"""
+        print response
+        return {USERNAME: response.get('id'),
+                'email': '',
+                'first_name': response.get('fullname')}
+
+class FlickrAuth(ConsumerBasedOAuth):
+    """Flickr OAuth authentication mechanism"""
+    AUTHORIZATION_URL = FLICKR_AUTHORIZATION_URL
+    REQUEST_TOKEN_URL = FLICKR_REQUEST_TOKEN_URL
+    ACCESS_TOKEN_URL = FLICKR_ACCESS_TOKEN_URL
+    SERVER_URL = FLICKR_SERVER
+    AUTH_BACKEND = FlickrBackend
+    SETTINGS_KEY_NAME = 'FLICKR_APP_ID'
+    SETTINGS_SECRET_NAME = 'FLICKR_API_SECRET'
+
+    def access_token(self, token):
+        """Return request for access token value"""
+        # Flickr is a bit different - it passes user information along with
+        # the access token, so temporarily store it to vie the user_data
+        # method easy access later in the flow!
+        request = self.oauth_request(token, self.ACCESS_TOKEN_URL)
+        response = self.fetch_response(request)
+        token = Token.from_string(response)
+        params = parse_qs(response)
+        token.user_nsid = params['user_nsid'][0]
+        token.fullname = params['fullname'][0]
+        token.username = params['username'][0]
+        return token
+
+    def user_data(self, access_token):
+        """Loads user data from service"""
+        return {
+            'id': access_token.user_nsid,
+            'username': access_token.username,
+            'fullname': access_token.fullname,
+        }
+
+# Backend definition
+BACKENDS = {
+    'flickr': FlickrAuth,
+}