Skip to content

Commit

Permalink
Fix a bug that caused get_application_access_token to break in v2.3+
Browse files Browse the repository at this point in the history
Fixes #140
  • Loading branch information
jgorset committed Jan 26, 2016
1 parent a4c4eb8 commit 8c52fea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
13 changes: 8 additions & 5 deletions facepy/utils.py
Expand Up @@ -60,9 +60,12 @@ def get_application_access_token(application_id, application_secret_key, api_ver
grant_type='client_credentials'
)

data = parse_qs(response)

try:
return data['access_token'][0]
except KeyError:
raise GraphAPI.FacebookError('No access token given')
data = parse_qs(response)

try:
return data['access_token'][0]
except KeyError:
raise GraphAPI.FacebookError('No access token given')
except AttributeError: # api_version >= 2.3 returns a dict
return response['access_token'], None
36 changes: 33 additions & 3 deletions tests/test_utils.py
Expand Up @@ -121,9 +121,12 @@ def test_get_extended_access_token_no_expiry():
@with_setup(mock, unmock)
def test_get_application_access_token():
mock_request.return_value.status_code = 200
mock_request.return_value.content = 'access_token=...'
mock_request.return_value.content = 'access_token=<application access token>'

access_token = get_application_access_token('<application id>', '<application secret key>')
access_token = get_application_access_token(
'<application id>',
'<application secret key>'
)

mock_request.assert_called_with(
'GET',
Expand All @@ -138,8 +141,35 @@ def test_get_application_access_token():
}
)

assert_equal(access_token, '...')
assert_equal(access_token, '<application access token>')

@with_setup(mock, unmock)
def test_get_application_access_token_v23_plus():
mock_request.return_value.status_code = 200
mock_request.return_value.content = (
'{"access_token":"<application access token>","token_type":"bearer"}'
)

access_token, expires_at = get_application_access_token(
'<application id>',
'<application secret key>',
api_version='2.3'
)

mock_request.assert_called_with(
'GET',
'https://graph.facebook.com/v2.3/oauth/access_token',
allow_redirects=True,
verify=True,
timeout=None,
params={
'client_id': '<application id>',
'client_secret': '<application secret key>',
'grant_type': 'client_credentials'
}
)

assert_equal(access_token, '<application access token>')

@with_setup(mock, unmock)
def test_get_application_access_token_raises_error():
Expand Down

0 comments on commit 8c52fea

Please sign in to comment.