Commits

Anonymous committed 893a0f3

signed request

  • Participants
  • Parent commits 52dc2ef

Comments (0)

Files changed (1)

File facepy/signed_request.py

 from datetime import datetime
 
 from facepy.exceptions import *
+import logging
 
+log = logging.getLogger(__name__)
 
 class SignedRequest(object):
     """
     raw = None
     """A string describing the signed request in its original format."""
 
-    def __init__(self, signed_request=None, application_secret_key=None):
+    def __init__(self, signed_request=None, application_secret_key=None, application_id=None, redirect_url = 'http://localhost:8000'):
         """
         Initialize a signed request.
 
         """
         self.signed_request = signed_request
         self.application_secret_key = application_secret_key
+        self.application_id = application_id
+        self.redirect_url = redirect_url
 
         self.raw = self.parse(signed_request, application_secret_key)
-        print self.raw
+        log.info(self.raw)
         self.data = self.raw.get('app_data', None)
 
         self.page = self.Page(
         if 'oauth_token' in self.raw:
             auth_token = self.raw["oauth_token"]
         else:
-            code = self.raw["code"]
-            from graph_api import GraphAPI
-            g = GraphAPI()
-            params = {
-                'code': code,
-                client_id=FACEBOOK_APP_ID,
-                client_secret = application_secret_key,
-
-            }
-            g.get("oauth/access_token",**params)
-            
+            code = self.raw.get("code")
+            log.info("code: %s" %code)
+            auth_token = self.__get_auth_token(code) 
 
         self.user = self.User(
             id=self.raw.get('user_id'),
             )
         )
 
+    def __get_auth_token(self,code):
+        assert code is not None, "NO hay code"
+        from graph_api import GraphAPI
+        from urlparse import parse_qs
+        g = GraphAPI()
+        FACEBOOK_APP_PASSWORD = self.application_secret_key
+        FACEBOOK_APP_ID = self.application_id
+        params = {
+            'code': code,
+            'client_id':FACEBOOK_APP_ID,
+            'client_secret': FACEBOOK_APP_PASSWORD,
+            'redirect_uri':"http://apps.facebook.com/int_quiniela_zod/", #self.redirect_url,
+        }
+        response = g.get("oauth/access_token", **params)
+        components = parse_qs(response)
+        access_token = components['access_token'][0]
+        return access_token
+
     def parse(cls, signed_request, application_secret_key):
         """Parse a signed request, returning a dictionary describing its payload."""
         def decode(encoded):