Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added FACEBOOK_APP_ID and FACEBOOK_APP_SECRET as kwargs #501

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions django_facebook/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ class OpenGraphShareAdmin(admin.ModelAdmin):
actions = [admin_actions.retry_open_graph_share,
admin_actions.retry_open_graph_share_for_user]

def view_share(self, instance):
def view_share(self, instance, *args, **kwargs):
share_id = instance.share_id
url_format = 'https://developers.facebook.com/tools/explorer/%s/?method=GET&path=%s%%2F'
url = url_format % (facebook_settings.FACEBOOK_APP_ID, share_id)
facebook_app_id = kwargs.get(facebook_settings.FACEBOOK_APP_ID_KWARGS, facebook_settings.FACEBOOK_APP_ID)
url = url_format % (facebook_app_id, share_id)
template = '<a href="%s">%s</a>' % (url, share_id)
return template
view_share.allow_tags = True
Expand Down
5 changes: 3 additions & 2 deletions django_facebook/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_persistent_graph(request, *args, **kwargs):
return graph


def get_facebook_graph(request=None, access_token=None, redirect_uri=None, raise_=False):
def get_facebook_graph(request=None, access_token=None, redirect_uri=None, raise_=False, *args, **kwargs):
'''
given a request from one of these
- js authentication flow (signed cookie)
Expand Down Expand Up @@ -137,6 +137,7 @@ def get_facebook_graph(request=None, access_token=None, redirect_uri=None, raise
if signed_data and 'oauth_token' in signed_data:
access_token = signed_data['oauth_token']

facebook_app_id = kwargs.get(facebook_settings.FACEBOOK_APP_ID_KWARGS, facebook_settings.FACEBOOK_APP_ID)
if not access_token:
# easy case, code is in the get
code = request.REQUEST.get('code')
Expand All @@ -145,7 +146,7 @@ def get_facebook_graph(request=None, access_token=None, redirect_uri=None, raise

if not code:
# signed request or cookie leading, base 64 decoding needed
cookie_name = 'fbsr_%s' % facebook_settings.FACEBOOK_APP_ID
cookie_name = 'fbsr_%s' % facebook_app_id
cookie_data = request.COOKIES.get(cookie_name)

if cookie_data:
Expand Down
11 changes: 7 additions & 4 deletions django_facebook/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@


def generate_oauth_url(scope=facebook_settings.FACEBOOK_DEFAULT_SCOPE,
next=None, extra_data=None):
next=None, extra_data=None, *args, **kwargs):
query_dict = QueryDict('', True)
canvas_page = (next if next is not None else
facebook_settings.FACEBOOK_CANVAS_PAGE)
query_dict.update(dict(client_id=facebook_settings.FACEBOOK_APP_ID,
facebook_app_id = kwargs.get(facebook_settings.FACEBOOK_APP_ID_KWARGS,
facebook_settings.FACEBOOK_APP_ID)
facebook_canvas_page = kwargs.get(facebook_settings.FACEBOOK_CANVAS_PAGE_KWARGS,
facebook_settings.FACEBOOK_CANVAS_PAGE)
canvas_page = (next if next is not None else facebook_canvas_page)
query_dict.update(dict(client_id=facebook_app_id,
redirect_uri=canvas_page,
scope=','.join(scope)))
if extra_data:
Expand Down
16 changes: 15 additions & 1 deletion django_facebook/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@

# : Your facebook app id
FACEBOOK_APP_ID = getattr(settings, 'FACEBOOK_APP_ID', None)

# : Keyword argument name for facebook app id
FACEBOOK_APP_ID_KWARGS = getattr(settings, 'FACEBOOK_APP_ID_KWARGS', 'facebook_app_id')

# : Your facebook app secret
FACEBOOK_APP_SECRET = getattr(settings, 'FACEBOOK_APP_SECRET', None)

# : Keyword argument name for facebook app secret
FACEBOOK_APP_SECRET_KWARGS = getattr(settings, 'FACEBOOK_APP_SECRET_KWARGS', 'facebook_app_secret')

# : The default scope we should use, note that registration will break without email
FACEBOOK_DEFAULT_SCOPE = getattr(settings, 'FACEBOOK_DEFAULT_SCOPE', [
'email', 'user_about_me', 'user_birthday', 'user_website'])

# : Keyword argument name for facebook app default scope
FACEBOOK_DEFAULT_SCOPE_KWARGS = getattr(settings, 'FACEBOOK_DEFAULT_SCOPE_KWARGS', 'facebook_default_scope')

# : If we should store likes
FACEBOOK_STORE_LIKES = getattr(settings, 'FACEBOOK_STORE_LIKES', False)
# : If we should store friends
Expand All @@ -28,10 +39,13 @@
FACEBOOK_REGISTRATION_BACKEND = getattr(
settings, 'FACEBOOK_REGISTRATION_BACKEND', default_registration_backend)

# Absolute canvas page url as per facebook standard
# : Absolute canvas page url as per facebook standard
FACEBOOK_CANVAS_PAGE = getattr(settings, 'FACEBOOK_CANVAS_PAGE',
'http://apps.facebook.com/django_facebook_test/')

# Keyword argument name for facebook app canvas page
FACEBOOK_CANVAS_PAGE_KWARGS = getattr(settings, 'FACEBOOK_CANVAS_PAGE_KWARGS', 'facebook_canvas_page')

# Disable this setting if you don't want to store a local image
FACEBOOK_STORE_LOCAL_IMAGE = getattr(
settings, 'FACEBOOK_STORE_LOCAL_IMAGE', True)
Expand Down
5 changes: 3 additions & 2 deletions django_facebook/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,15 @@ def queryset_iterator(queryset, chunksize=1000, getfunc=getattr):
gc.collect()


def get_oauth_url(scope, redirect_uri, extra_params=None):
def get_oauth_url(scope, redirect_uri, extra_params=None, *args, **kwargs):
'''
Returns the oAuth URL for the given scope and redirect_uri
'''
scope = parse_scope(scope)
facebook_app_id = kwargs.get(facebook_settings.FACEBOOK_APP_ID_KWARGS, facebook_settings.FACEBOOK_APP_ID)
query_dict = QueryDict('', True)
query_dict['scope'] = ','.join(scope)
query_dict['client_id'] = facebook_settings.FACEBOOK_APP_ID
query_dict['client_id'] = facebook_app_id

query_dict['redirect_uri'] = redirect_uri
oauth_url = 'https://www.facebook.com/dialog/oauth?'
Expand Down
5 changes: 3 additions & 2 deletions django_facebook/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@

@csrf_exempt
@facebook_required_lazy
def connect(request, graph):
def connect(request, graph, *args, **kwargs):
'''
Exception and validation functionality around the _connect view
Separated this out from _connect to preserve readability
Don't bother reading this code, skip to _connect for the bit you're interested in :)
'''
backend = get_registration_backend()
context = RequestContext(request)
facebook_app_id = kwargs.get(facebook_settings.FACEBOOK_APP_ID_KWARGS, facebook_settings.FACEBOOK_APP_ID)

# validation to ensure the context processor is enabled
if not context.get('FACEBOOK_APP_ID'):
if not context.get('FACEBOOK_APP_ID') and not facebook_app_id:
message = 'Please specify a Facebook app id and ensure the context processor is enabled'
raise ValueError(message)

Expand Down
54 changes: 33 additions & 21 deletions open_facebook/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,9 @@ class FacebookAuthorization(FacebookConnection):
'''
@classmethod
def convert_code(cls, code,
redirect_uri='http://local.mellowmorning.com:8000/facebook/connect/'):
redirect_uri='http://local.mellowmorning.com:8000/facebook/connect/',
client_id=facebook_settings.FACEBOOK_APP_SECRET,
client_secret=facebook_settings.FACEBOOK_APP_SECRET):
'''
Turns a code into an access token

Expand All @@ -420,14 +422,16 @@ def convert_code(cls, code,
:returns: dict

'''
kwargs = cls._client_info()
kwargs = cls._client_info(client_id, client_secret)
kwargs['code'] = code
kwargs['redirect_uri'] = redirect_uri
response = cls.request('oauth/access_token', **kwargs)
return response

@classmethod
def extend_access_token(cls, access_token):
def extend_access_token(cls, access_token,
client_id=facebook_settings.FACEBOOK_APP_SECRET,
client_secret=facebook_settings.FACEBOOK_APP_SECRET):
'''
https://developers.facebook.com/roadmap/offline-access-removal/
We can extend the token only once per day
Expand All @@ -443,17 +447,15 @@ def extend_access_token(cls, access_token):

:returns: dict
'''
kwargs = cls._client_info()
kwargs = cls._client_info(client_id, client_secret)
kwargs['grant_type'] = 'fb_exchange_token'
kwargs['fb_exchange_token'] = access_token
response = cls.request('oauth/access_token', **kwargs)
return response

@classmethod
def _client_info(cls):
kwargs = dict(client_id=facebook_settings.FACEBOOK_APP_ID)
kwargs['client_secret'] = facebook_settings.FACEBOOK_APP_SECRET
return kwargs
def _client_info(cls, client_id, client_secret):
return dict(client_id=client_id, client_secret=client_secret)

@classmethod
def parse_signed_data(cls, signed_request,
Expand Down Expand Up @@ -495,20 +497,25 @@ def parse_signed_data(cls, signed_request,
return data

@classmethod
def get_app_access_token(cls):
def get_app_access_token(cls, *args, **kwargs):
'''
Get the access_token for the app that can be used for
insights and creating test users
application_id = retrieved from the developer page
application_secret = retrieved from the developer page
returns the application access_token
'''
kwargs = {
facebook_app_id = kwargs.get(facebook_settings.FACEBOOK_APP_ID_KWARGS,
facebook_settings.FACEBOOK_APP_ID)
facebook_app_secret = kwargs.get(facebook_settings.FACEBOOK_APP_SECRET_KWARGS,
facebook_settings.FACEBOOK_APP_SECRET)

data = {
'grant_type': 'client_credentials',
'client_id': facebook_settings.FACEBOOK_APP_ID,
'client_secret': facebook_settings.FACEBOOK_APP_SECRET,
'client_id': facebook_app_id,
'client_secret': facebook_app_secret,
}
response = cls.request('oauth/access_token', **kwargs)
response = cls.request('oauth/access_token', **data)
return response['access_token']

@memoized
Expand All @@ -521,7 +528,7 @@ def get_cached_app_access_token(cls):
return app_access_token

@classmethod
def create_test_user(cls, app_access_token, permissions=None, name=None):
def create_test_user(cls, app_access_token, permissions=None, name=None, *args, **kwargs):
'''
Creates a test user with the given permissions and name

Expand All @@ -544,16 +551,19 @@ def create_test_user(cls, app_access_token, permissions=None, name=None):
',', ' ').replace('_', '')
name = name or default_name

kwargs = {
facebook_app_id = kwargs.get(facebook_settings.FACEBOOK_APP_ID_KWARGS,
facebook_settings.FACEBOOK_APP_ID)

data = {
'access_token': app_access_token,
'installed': True,
'name': name,
'method': 'post',
'permissions': permissions,
}
path = '%s/accounts/test-users' % facebook_settings.FACEBOOK_APP_ID
path = '%s/accounts/test-users' % facebook_app_id
# add the test user data to the test user data class
test_user_data = cls.request(path, **kwargs)
test_user_data = cls.request(path, **data)
test_user_data['name'] = name
test_user = TestUser(test_user_data)

Expand Down Expand Up @@ -615,11 +625,13 @@ def get_or_create_test_user(cls, app_access_token, name=None, permissions=None,
return test_user

@classmethod
def get_test_users(cls, app_access_token):
kwargs = dict(access_token=app_access_token)
path = '%s/accounts/test-users' % facebook_settings.FACEBOOK_APP_ID
def get_test_users(cls, app_access_token, *args, **kwargs):
data = dict(access_token=app_access_token)
facebook_app_id = kwargs.get(facebook_settings.FACEBOOK_APP_ID_KWARGS,
facebook_settings.FACEBOOK_APP_ID)
path = '%s/accounts/test-users' % facebook_app_id
# retrieve all test users
response = cls.request(path, **kwargs)
response = cls.request(path, **data)
test_users = response['data']
return test_users

Expand Down