Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

checking for expired token

  • Loading branch information...
commit 8585ce223e18cf19f410b4e6aa56b1478913162e 1 parent b172d9c
Aida Tavakkolie authored June 01, 2011

Showing 1 changed file with 19 additions and 9 deletions. Show diff stats Hide diff stats

  1. 28  django_facebook/api.py
28  django_facebook/api.py
@@ -4,11 +4,13 @@
4 4
 from django.utils import simplejson as json
5 5
 from django_facebook import settings as facebook_settings
6 6
 from django_facebook.official_sdk import GraphAPI, GraphAPIError
7  
-import datetime
  7
+from datetime import datetime
8 8
 import hashlib
9 9
 import hmac
10 10
 import logging
11 11
 import sys
  12
+import time
  13
+
12 14
 
13 15
 logger = logging.getLogger(__name__)
14 16
 logger.setLevel(logging.DEBUG)
@@ -31,41 +33,49 @@ def get_facebook_graph(request=None, access_token=None, persistent_token=faceboo
31 33
     
32 34
     additional_data = None
33 35
     facebook_open_graph_cached = False
34  
-    
  36
+
35 37
     if persistent_token:
36  
-        logger.debug('CACHED facebook_open_graph ')
  38
+        logger.debug('Get cached facebook_open_graph ')
37 39
         facebook_open_graph_cached = request.session.get('facebook_open_graph')
38 40
     if facebook_open_graph_cached:
39 41
         #TODO: should handle this in class' pickle protocol, but this is easier
40 42
         facebook_open_graph_cached._is_authenticated = None
41 43
         
42  
-    signed_request = request.REQUEST.get('signed_request') or request.COOKIES.get('signed_request')
  44
+    signed_request = request.REQUEST.get('signed_request')
43 45
     cookie_name = 'fbs_%s' % facebook_settings.FACEBOOK_APP_ID
44 46
     oauth_cookie = request.COOKIES.get(cookie_name)
45  
-    
  47
+                
46 48
     #scenario A, we're on a canvas page and need to parse the signed data
47 49
     if signed_request:
48 50
         logger.debug('found signed request..')
49 51
         additional_data = FacebookAPI.parse_signed_data(signed_request)
50  
-        logger.debug(additional_data)
  52
+        logger.debug('signed_data: '+str(additional_data))
51 53
         access_token = additional_data.get('oauth_token')
52 54
     #scenario B, we're using javascript and cookies to authenticate
53 55
     elif oauth_cookie:
54 56
         logger.debug('found oauth cookie...')
55 57
         additional_data = official_sdk.get_user_from_cookie(request.COOKIES, facebook_settings.FACEBOOK_APP_ID, facebook_settings.FACEBOOK_APP_SECRET)
56 58
         additional_data["user_id"] = additional_data["uid"]
57  
-        logger.debug(additional_data)
  59
+        logger.debug('oauth cookie: signed_data: '+str(additional_data))
58 60
         access_token = additional_data.get('access_token')
59 61
     
60 62
     facebook_open_graph = FacebookAPI(access_token, additional_data)
61  
-    
  63
+
  64
+
62 65
     if facebook_open_graph.access_token and persistent_token:
63 66
         logger.debug('storing facebook_open_graph in session..')
64 67
         request.session['facebook_open_graph'] = facebook_open_graph
65 68
     elif facebook_open_graph_cached:
  69
+        logger.debug('Using cached facebook_open_graph ')
66 70
         facebook_open_graph = facebook_open_graph_cached
67 71
 
68  
-        
  72
+
  73
+    # Check if the OAuth token has expired
  74
+    if facebook_open_graph.additional_data.has_key('expires'):
  75
+        if datetime.fromtimestamp(float(facebook_open_graph.additional_data['expires'])) < datetime.now():
  76
+            logger.debug('FB Access token expired, revalidate!')
  77
+            facebook_open_graph._is_authenticated = None
  78
+
69 79
     return facebook_open_graph
70 80
 
71 81
 

1 note on commit 8585ce2

Thierry Schellenbach

Facebook is changing their api soon, you need to update before oct 1st

Please sign in to comment.
Something went wrong with that request. Please try again.