Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement get_extended_access_token

  • Loading branch information...
commit b5153f460f2f52cef9a5e49a3b48b3fb8742356c 1 parent e79fec5
Johannes Gorset authored
6 docs/source/usage/utilities.rst
View
@@ -0,0 +1,6 @@
+.. _utilities:
+
+Utilities
+=========
+
+.. autofunction:: facepy.utils.get_extended_access_token
2  facepy/__init__.py
View
@@ -1,5 +1,5 @@
from .graph_api import GraphAPI
from .signed_request import SignedRequest
-from .utils import get_application_access_token
+from .utils import get_application_access_token, get_extended_access_token
from .exceptions import FacepyError
from .version import __version__
28 facepy/utils.py
View
@@ -1,7 +1,35 @@
from urlparse import parse_qs
+from datetime import datetime, timedelta
from .graph_api import GraphAPI
+def get_extended_access_token(access_token, application_id, application_secret_key):
+ """
+ Get an extended OAuth access token.
+
+ :param access_token: A string describing an OAuth access token.
+ :param application_id: An integer describing the Facebook application's ID.
+ :param application_secret_key: A string describing the Facebook application's secret key.
+
+ Returns a tuple with a string describing the extended access token and a datetime instance
+ describing when it expires.
+ """
+ graph = GraphAPI()
+
+ response = graph.get('oauth/access_token',
+ client_id = application_id,
+ client_secret = application_secret_key,
+ grant_type = 'fb_exchange_token',
+ fb_exchange_token = access_token
+ )
+
+ components = parse_qs(response)
+
+ token = components['access_token'][0]
+ expires_at = datetime.now() + timedelta(seconds = int(components['expires'][0]))
+
+ return token, expires_at
+
def get_application_access_token(application_id, application_secret_key):
"""
Query Facebook for an OAuth access token for the given application.
21 tests/test_utils.py
View
@@ -1,5 +1,7 @@
"""Tests for the ``utils`` module."""
+from datetime import datetime
+
from mock import patch
from nose.tools import *
@@ -16,6 +18,25 @@ def unmock():
patch.stop()
@with_setup(mock, unmock)
+def test_get_extended_access_token():
+ mock_request.return_value.content = 'access_token=<extended access token>&expires=5183994'
+
+ access_token, expires_at = get_extended_access_token('<access token>', '<application id>', '<application secret key>')
+
+ mock_request.assert_called_with('GET', 'https://graph.facebook.com/oauth/access_token',
+ allow_redirects = True,
+ params = {
+ 'client_id': '<application id>',
+ 'client_secret': '<application secret key>',
+ 'grant_type': 'fb_exchange_token',
+ 'fb_exchange_token': '<access token>'
+ }
+ )
+
+ assert_equal(access_token, '<extended access token>')
+ assert isinstance(expires_at, datetime)
+
+@with_setup(mock, unmock)
def test_get_application_access_token():
mock_request.return_value.content = 'access_token=...'
Please sign in to comment.
Something went wrong with that request. Please try again.