Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extensions/cpsection/webservices/services/facebook/service.py
blob: 01b9d4e196c10251b72946be6fd87f51739773e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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 time
import urllib
import urlparse

from gi.repository import GConf
from gi.repository import WebKit
from web.facebook import account
from cpsection.webservices.web_service import WebService


class WebService(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 'access_token' in params and 'expires_in' in params:
            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(account.Account.ACCESS_TOKEN_KEY,
                          access_token)

        expiry_time = int(time.time()) + expires_in
        client.set_int(
            account.Account.ACCESS_TOKEN_KEY_EXPIRATION_DATE,
            expiry_time)


def get_service():
    return WebService()