Permalink
Browse files

Implement get_extended_access_token

  • Loading branch information...
1 parent e79fec5 commit b5153f460f2f52cef9a5e49a3b48b3fb8742356c @jgorset committed Aug 22, 2012
Showing with 56 additions and 1 deletion.
  1. +6 −0 docs/source/usage/utilities.rst
  2. +1 −1 facepy/__init__.py
  3. +28 −0 facepy/utils.py
  4. +21 −0 tests/test_utils.py
@@ -0,0 +1,6 @@
+.. _utilities:
+
+Utilities
+=========
+
+.. autofunction:: facepy.utils.get_extended_access_token
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__
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.
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=...'

0 comments on commit b5153f4

Please sign in to comment.