Commits

ronald martinez committed b21ad01

ok

Comments (0)

Files changed (6)

+-----BEGIN CERTIFICATE-----
+MIID6zCCAtOgAwIBAgIJAOONSLVmnNtrMA0GCSqGSIb3DQEBBQUAMIGLMQswCQYD
+VQQGEwJQRTENMAsGA1UECAwETGltYTENMAsGA1UEBwwETGltYTERMA8GA1UECgwI
+RnVuY2l0b24xFzAVBgNVBAsMDkNvbW11bmljYXRpb25zMQ8wDQYDVQQDDAZSb25h
+bGQxITAfBgkqhkiG9w0BCQEWEm1hY2ZyaTEwQGdtYWlsLmNvbTAeFw0xMjExMjYx
+ODE1MDJaFw0xNTExMjYxODE1MDJaMIGLMQswCQYDVQQGEwJQRTENMAsGA1UECAwE
+TGltYTENMAsGA1UEBwwETGltYTERMA8GA1UECgwIRnVuY2l0b24xFzAVBgNVBAsM
+DkNvbW11bmljYXRpb25zMQ8wDQYDVQQDDAZSb25hbGQxITAfBgkqhkiG9w0BCQEW
+Em1hY2ZyaTEwQGdtYWlsLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
+ggEBALLVqkhZqpljvS/1tkDz0Bs0Ak9veFI9F5mWLRybicOnULXxeytD3+y82cpL
+nQnoXVFUSMH3rELTGxKs+FfaP3uYTXyGX5TMtltm/R/vCOcGjW1f8sIsngzDG1ve
+C4Qi0UuoQ/D97MOa/GxAVIE2iRS694EknfuP6jVyU8RuU95FSoO1cyQbOV64yOqZ
+vZmB18i3hAW+v79SQ9XifzBxjklXvwvP0ITlz/rffFxrb3nxCH+Kyb15TZkpGd6b
+dBpcOz+PiX8u1VSjcFLd9sPib7LAeleCa6BfukpTAkSsZoPYF4dN52rMXcpww3AR
+2aEQ0TjirNqgQNn7ez/oQ2V01j8CAwEAAaNQME4wHQYDVR0OBBYEFFSE40hkn3X8
+qSorqQWRWdwQKGW6MB8GA1UdIwQYMBaAFFSE40hkn3X8qSorqQWRWdwQKGW6MAwG
+A1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAG8/u+GY6Kg4fgd24HkEWgba
+RnaYBzxhrrGbLkbLKUEdbftAuyN9J5hB+Fex/crFKfp4Q8cp6jUimFkYkVr6ovdr
+Cp7825Rm6ta5trGiYB89RgpfjpW8hZGdFwVDrzjCINRcJHvrrekJ6TnLfjvYlSG+
+zjwJaJSGlYKdWUfEQkh3UWOGTBtkEWrcDtVz0GKAaprg4O3LvwLxQfJfbqbW705n
+i3KNec1c+Fs2MUUxljRd5+FATfDQTwJRKSdOdFg8ucWdHa042quSa2Q1Bi9kkogg
+zT4mQvOUOsmINZzTa7VETUm0q2CxnDMgiQa8IGWqStbsNSDeALjmqpAopdVTRUY=
+-----END CERTIFICATE-----

controllers/gift.py

                 json=True
             )
 
+
+            from controllers.user import window_authorize_location
+
+            if 'error' in data:
+                if data.get('error').get('type') == 'OAuthException':
+                    self.finish(window_authorize_location('friends'))
+
+            #print data
+
             skus = [str(x.get('name')) for x in data.get('data') \
                 if x.get('fql_result_set')]
 

controllers/user.py

 from controllers import Session, BaseHandler
 from tornado.web import HTTPError, asynchronous
 from tornado.httpclient import AsyncHTTPClient
+from tornado.ioloop import IOLoop
 
 
-from tornado.ioloop import IOLoop
+def window_location(redirect):
+
+    url = '%s%s' % (settings.FACEBOOK_CANVAS_PAGE, redirect)
+    return "<script>window.top.location='%s'</script>" % url
+
+
+def window_authorize_location(redirect=None):
+
+    url = '%s%s%s' % (settings.FACEBOOK_CANVAS_PAGE, 'authorize/', redirect)
+    return "<script>window.top.location='%s'</script>" % url
+
+
+def authorize_permission(redirect):
+
+    permissions = (
+        'email',
+        'friends_likes',
+        'friends_activities',
+        'friends_interests',
+        'friends_birthday',
+        'read_stream',
+        'user_birthday'
+    )
+
+    params = dict(
+        client_id=settings.FACEBOOK_API_KEY,
+        redirect_uri='%sauthorize/%s' % (
+            settings.FACEBOOK_CANVAS_PAGE, redirect),
+        scope=','.join(permissions)
+    )
+
+    url = '%s?%s' % (settings.FACEBOOK_OAUTH, urllib.urlencode(params))
+    return "<script>window.top.location='%s'</script>" % url
 
 
 def get_age(date_of_birth):
         return data
 
 
-class Index(BaseHandler):
+class Authorize(BaseHandler):
 
     def check_xsrf_cookie(self):
         pass
 
-    def _on_ok(self):
-        print "ok"
+    def post(self, redirect):
+
+        #print "PPPP"
+
+        signed_request = self.get_argument('signed_request', None)
+
+        if not signed_request:
+            raise HTTPError(500)
+
+        user_data_facebook = parse_signed_request(
+            signed_request,
+            settings.FACEBOOK_API_SECRET
+        )
+
+        print user_data_facebook
+
         return
 
-    def _on_update_user(self):
+        if not 'user_id' in user_data_facebook:
+            self.finish(authorize_permission(redirect))
+        else:
+
+            data = cPickle.dumps({
+                'user_id': user_data_facebook['user_id'],
+                'access_token': user_data_facebook['oauth_token'],
+                'expires': user_data_facebook['expires']
+            }, -1)
+
+            self.set_secure_cookie('user', data)
+
+            IOLoop.instance().add_timeout(
+                time.time() + 2,
+                self._update_user
+            )
+
+            if redirect=='index':
+                redirect = ''
+
+            self.finish(window_location(redirect))
+
+
+class Index(BaseHandler):
+
+    def check_xsrf_cookie(self):
+        pass
+
+    @asynchronous
+    def post(self):
+
+        cookie = self.get_secure_cookie('user')
+
+        if not cookie:
+            self.finish(window_authorize_location('index'))
+        else:
+            user_data_facebook = cPickle.loads(cookie) if cookie else None
+
+            print user_data_facebook
+
+            self.finish(window_location('friends'))
+
+    def _update_user(self):
 
         cookie = self.get_secure_cookie('user')
         user_data_facebook = cPickle.loads(cookie) if cookie else None
             )
 
             http_client = AsyncHTTPClient()
-            http_client.fetch(url, self.on_update_user)
+            http_client.fetch(url, self.add_user)
 
         else:
 
+            print "exists"
+            """
             try:
                 user.update({'access_token': user_data_facebook.get(
                     'access_token')})
                 logging.error(exc)
             else:
                 logging.info('update user token')
-
-    @asynchronous
-    def post(self):
-
-        signed_request = self.get_argument('signed_request', None)
-
-        if not signed_request:
-            raise HTTPError(500)
-
-        user_data_facebook = parse_signed_request(
-            signed_request,
-            settings.FACEBOOK_API_SECRET
-        )
-
-        if not 'user_id' in user_data_facebook:
-
-            permissions = (
-                'email',
-                'friends_likes',
-                'friends_activities',
-                'friends_interests',
-                'friends_birthday',
-                'read_stream',
-                'user_birthday'
-            )
-
-            params = dict(
-                client_id=settings.FACEBOOK_API_KEY,
-                redirect_uri=settings.FACEBOOK_CANVAS_PAGE,
-                scope=','.join(permissions)
-            )
-
-            url = '%s?%s' % (settings.FACEBOOK_OAUTH, urllib.urlencode(params))
-            self.write("<script>top.location.href='%s'</script>" % url)
-
-        else:
-
-            data = cPickle.dumps({
-                'user_id': user_data_facebook['user_id'],
-                'access_token': user_data_facebook['oauth_token']
-            }, -1)
-
-            self.set_secure_cookie('user', data)
-
-            IOLoop.instance().add_timeout(
-                time.time() + 5,
-                self._on_update_user
-            )
-
-            """
-            user = Session.query(User).filter_by(
-                fbid=user_data_facebook.get('user_id'))
-
-            if not user.first():
-
-                params = dict(
-                    access_token=user_data_facebook.get('oauth_token'),
-                    fields='email,birthday,gender,name'
-                )
-
-                url = '%s?%s' % (
-                    '%s/%s' % (
-                        settings.FACEBOOK_GRAPH,
-                        user_data_facebook.get('user_id')
-                    ),
-                     urllib.urlencode(params)
-                )
-
-                http_client = AsyncHTTPClient()
-                http_client.fetch(url, self.on_update_user)
-
-            else:
-
-                try:
-                    user.update({'access_token': user_data_facebook.get(
-                        'oauth_token')})
-                except Exception as exc:
-                    logging.error(exc)
-                else:
-                    logging.info('update user token')
             """
 
-            self.redirect(self.reverse_url('list_friends'))
-
-    def on_update_user(self, user_data_facebook_graph):
+    def add_user(self, user_data_facebook_graph):
 
         if user_data_facebook_graph.error:
             raise HTTPError(500)
             else:
                 logging.info('save user')
 
+        self.finish()
+
 
 class ListFriends(BaseHandler):
 
-    def get(self):
+    def check_xsrf_cookie(self):
+        pass
+
+    def post(self):
 
         cookie = self.get_secure_cookie('user')
         cookie_user = cPickle.loads(cookie) if cookie else None
             self.get_argument('page', '1')
         )
 
+        at = 'AAAG1OBVnjW8BAEQP8V5bgKqwhZB1OU5ejqtxKhth8rQx9ZAgeEbauVZCe7rHEclfZBXfJMSPo3ZCqzFDWyzemLSlbCZCceAZC8FkQzLafW43gZDZD'
+        #at = cookie_user.get('access_token')
+
         params = dict(
-            access_token=cookie_user.get('access_token'),
+            access_token=at,
             fields='name',
             limit=limit,
             offset=offset
             json=True
         )
 
+        if 'error' in data:
+
+            if data.get('error').get('type') == 'OAuthException':
+                self.finish(window_authorize_location('friends'))
+            else:
+                raise HTTPError(500)
+
         self.render('index.html', data=data.get('data'))
+-----BEGIN RSA PRIVATE KEY-----
+MIIEowIBAAKCAQEAstWqSFmqmWO9L/W2QPPQGzQCT294Uj0XmZYtHJuJw6dQtfF7
+K0Pf7LzZykudCehdUVRIwfesQtMbEqz4V9o/e5hNfIZflMy2W2b9H+8I5waNbV/y
+wiyeDMMbW94LhCLRS6hD8P3sw5r8bEBUgTaJFLr3gSSd+4/qNXJTxG5T3kVKg7Vz
+JBs5XrjI6pm9mYHXyLeEBb6/v1JD1eJ/MHGOSVe/C8/QhOXP+t98XGtvefEIf4rJ
+vXlNmSkZ3pt0Glw7P4+Jfy7VVKNwUt32w+JvssB6V4JroF+6SlMCRKxmg9gXh03n
+asxdynDDcBHZoRDROOKs2qBA2ft7P+hDZXTWPwIDAQABAoIBACXLFdN4b58+/spV
+6RBM9lvpX9uduRXYJ1aj8CCtli2s/FeXEuqPy5DiUDx6QlPHsYDwiPfE+bidmIkX
+2NvoGaZrB2UbmLN/l8I1P6UyBPWd4uGMd2clurkDupJtmI1yNj0A8GrLnu+vn0zZ
+Q4KTHeEtqgS20lRJk7t7z8whbMP6Ed24DJTA3wiFw+qi+iPcwupCcAMqdHS7pCy5
+b/iheQEdb7+uIlNvWHLcwiRczsoNxCZoWN/hm0MpFR8jIzqEDi47BLYnEG0cNXDr
+dIKYnDMpcExQ+P0QhtW2dW30hzjFG6kd1rOSPey0V+BWRUS896l7ZFHry2SOguNd
+LKrLysECgYEA7Q+BhXRn9FyfqwGCeMWa8UhO7UMw0ayJQSifmMAMiPBMVMzKEeGy
+1hWp8oELLcKybp4w2cryC/T0Uo9LDkHRlsyYg/XLShEqEdAnV+3k2TnmuJO14pUC
+Ocx+Pe84eSfQpCzk25HJ8KY7BBdSKmQLUU92IsH4LgOXjfO5YvCGKRECgYEAwR9K
+QFSyRn+DnLOejjrNhryj/ZxlMuKKcdTS4DC1ukr9IS2Ag39YwlRPp+2Cbiv6SAfv
+q0Hs+Eihmw4/FxyJvgZ6539We9hLJkyTjH/C+sTmQwt0mjvNODczA1Gb536f5Yqp
+hV7s4jM2vB4whENzEF9eKakpEN8nRgWcaTM7ik8CgYAaQ7aI76240C5Mhv1KnjId
+e3JrLR+Si3glhPext81rupvqZUoaExR6TaonmQ6BAmo8gUBOUQLiOkB7iS66pKYw
++/gF3syoJyCvGo3RWyUYwCaE2rCzKuk1OCfgesmZ+sRN/D9a6FskU3aEbIUI906d
+sp0XGsCBcJpw+sjWYmmq0QKBgQCkQjbnbKKNb9vDpssPclhpZ32cMi+pDiJvG3OZ
+GBUZtLqEQ61xKxmVgByYWl1704/3QDjL0zD5JN2/gmiLAczugZh72kT25UtV8f/1
+mp1+qYaoM9AjdwXf8KNFCV6vkXXyyPPS6qBU7PZ7HrppZO0zWco/aQ/yt7sxxYf5
+uofD5QKBgHfmjQI2rTqg8oXSBijHKVDn2rzooY5TdQC8VXaUn1cSrOgxdsbUdsub
+J0NyuD0fOfYOJ+Lo56GLZBtfqkUvxjKwerQBDHjr9whikIn/nc+Jgt/JePMoMcgB
+1/F1xszl7ROqDZp4pCqDN2iSFl83l2VxELm66B//g7ptNJ8HenV7
+-----END RSA PRIVATE KEY-----
 
 handlers = (
     url(
+        '/authorize/(.*)',
+        user.Authorize,
+        name='authorize'
+    ), 
+    url(
         '/friends',
         user.ListFriends,
         name='list_friends'
 FACEBOOK_API_KEY = '480727458614639'
 FACEBOOK_API_SECRET = 'fd011c82c4e37bde6622fd1581d2124d'
 
-FACEBOOK_CANVAS_PAGE = 'http://apps.facebook.com/saga-find-gift/'
+FACEBOOK_CANVAS_PAGE = 'https://apps.facebook.com/saga-find-gift/'
 FACEBOOK_GRAPH = 'https://graph.facebook.com/'
 FACEBOOK_OAUTH = 'https://www.facebook.com/dialog/oauth/'
 FACEBOOK_ACCESS_TOKEN = 'https://graph.facebook.com/oauth/access_token'