Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extensions/cpsection/webservices/services/facebook/service.py
diff options
context:
space:
mode:
authorWalter Bender <walter.bender@gmail.com>2013-02-18 18:24:50 (GMT)
committer Walter Bender <walter.bender@gmail.com>2013-02-18 18:24:50 (GMT)
commit0cbe5e4d777dd3b2b2fac517a7279e0358650b76 (patch)
treef98dff0753803449aff3e635671538343a560e1c /extensions/cpsection/webservices/services/facebook/service.py
parent4bcc50dc68977f99789a0bd86aadd39ec5357c7b (diff)
generalize cpsection/webservices code
Diffstat (limited to 'extensions/cpsection/webservices/services/facebook/service.py')
-rw-r--r--extensions/cpsection/webservices/services/facebook/service.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/extensions/cpsection/webservices/services/facebook/service.py b/extensions/cpsection/webservices/services/facebook/service.py
new file mode 100644
index 0000000..59c14c4
--- /dev/null
+++ b/extensions/cpsection/webservices/services/facebook/service.py
@@ -0,0 +1,90 @@
+# Copyright (C) 2013, Walter Bender - Raul Gutierrez Segales
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import logging
+import os
+import time
+import urllib
+import urlparse
+
+from gi.repository import GConf
+from gi.repository import Gtk
+from gi.repository import GObject
+from gi.repository import WebKit
+from gettext import gettext as _
+from jarabe import config
+from web.fb import facebook_online_account as fboa
+from cpsection.webservices.web_service import WebService
+
+
+class FacebookService(WebService):
+ FB_APP_ID = "172917389475707"
+ FB_REDIRECT_URI = "http://www.sugarlabs.org"
+
+ def get_icon_name(self):
+ return 'facebook-share'
+
+ def config_service_cb(self, widget, event, container):
+ logging.debug('config_service_fb')
+
+ wkv = WebKit.WebView()
+ wkv.load_uri(self._fb_auth_url())
+ wkv.grab_focus()
+ wkv.connect('navigation-policy-decision-requested',
+ self._fb_nav_policy_cb)
+
+ for c in container.get_children():
+ container.remove(c)
+
+ container.add(wkv)
+ container.show_all()
+
+ def _fb_auth_url(self):
+ url = 'http://www.facebook.com/dialog/oauth'
+ params = [
+ ('client_id', self.FB_APP_ID),
+ ('redirect_uri', self.FB_REDIRECT_URI),
+ ('response_type', 'token'),
+ ('scope', 'publish_stream')
+ ]
+
+ return "%s?%s" % (url, urllib.urlencode(params))
+
+ def _fb_nav_policy_cb(self, view, frame, req, action, param):
+ uri = req.get_uri()
+ if uri is None:
+ return
+
+ url_o = urlparse.urlparse(uri)
+ params = urlparse.parse_qs(url_o.fragment)
+ if params.has_key('access_token') and params.has_key('expires_in'):
+ self._fb_save_access_token(params['access_token'][0],
+ int(params['expires_in'][0]))
+
+ def _fb_save_access_token(self, access_token, expires_in):
+ client = GConf.Client.get_default()
+
+ client.set_string(fboa.FacebookOnlineAccount.ACCESS_TOKEN_KEY,
+ access_token)
+
+ expiry_time = int(time.time()) + expires_in
+ client.set_int(
+ fboa.FacebookOnlineAccount.ACCESS_TOKEN_KEY_EXPIRATION_DATE,
+ expiry_time)
+
+
+def get_service():
+ return FacebookService()