Skip to content

Commit

Permalink
Add Flickr backend
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Oct 10, 2011
1 parent 872cc20 commit 3ff3ad0
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.rst
Expand Up @@ -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
Expand Down Expand Up @@ -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',
)
Expand Down Expand Up @@ -158,6 +160,8 @@ Configuration
GITHUB_API_SECRET = ''
DROPBOX_APP_ID = ''
DROPBOX_API_SECRET = ''
FLICKR_APP_ID = ''
FLICKR_API_SECRET = ''

- Setup login URLs::

Expand Down Expand Up @@ -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
-------
Expand Down Expand Up @@ -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/
80 changes: 80 additions & 0 deletions social_auth/backends/contrib/flickr.py
@@ -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,
}

0 comments on commit 3ff3ad0

Please sign in to comment.