From 8c52fea73165dd4269277cf0beffb2583538272a Mon Sep 17 00:00:00 2001 From: Johannes Gorset Date: Tue, 26 Jan 2016 19:59:37 +0100 Subject: [PATCH] Fix a bug that caused get_application_access_token to break in v2.3+ Fixes #140 --- facepy/utils.py | 13 ++++++++----- tests/test_utils.py | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/facepy/utils.py b/facepy/utils.py index 2aabeca..2307274 100644 --- a/facepy/utils.py +++ b/facepy/utils.py @@ -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 diff --git a/tests/test_utils.py b/tests/test_utils.py index a89b18b..d225fe6 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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=' - access_token = get_application_access_token('', '') + access_token = get_application_access_token( + '', + '' + ) mock_request.assert_called_with( 'GET', @@ -138,8 +141,35 @@ def test_get_application_access_token(): } ) - assert_equal(access_token, '...') + assert_equal(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":"","token_type":"bearer"}' + ) + + access_token, expires_at = get_application_access_token( + '', + '', + 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': '', + 'client_secret': '', + 'grant_type': 'client_credentials' + } + ) + assert_equal(access_token, '') @with_setup(mock, unmock) def test_get_application_access_token_raises_error():