1
from gi.repository import GLib, Gtk, Pango, WebKit
2
import urllib, urllib2, json, urlparse, uuid
3
from oauth import oauth
5
from gwibber.microblog.util import resources
6
from gwibber.microblog.util.keyring import get_from_keyring
8
from gettext import gettext as _
9
if hasattr(gettext, 'bind_textdomain_codeset'):
10
gettext.bind_textdomain_codeset('gwibber','UTF-8')
11
gettext.textdomain('gwibber')
15
sigmeth = oauth.OAuthSignatureMethod_HMAC_SHA1()
17
CLIENT_ID = "BA0GOA0K3PTRS1KUJ5TTZ1P3GDRH3VJEEXY4N44ROPUJYKPW"
18
CLIENT_SECRET = "TS1LGTEZSJATQG22K5SXNASF021KPXI5LJTBQOVTX4JGOTYH"
19
REDIRECT_URI = "http://gwibber.com/0/auth.html"
21
class AccountWidget(Gtk.VBox):
22
"""AccountWidget: A widget that provides a user interface for configuring foursquare accounts in Gwibber
25
def __init__(self, account=None, dialog=None):
26
"""Creates the account pane for configuring Foursquare accounts"""
27
Gtk.VBox.__init__( self, False, 20 )
28
self.ui = Gtk.Builder()
29
self.ui.set_translation_domain ("gwibber")
30
self.ui.add_from_file (resources.get_ui_asset("gwibber-accounts-foursquare.ui"))
31
self.ui.connect_signals(self)
32
self.vbox_settings = self.ui.get_object("vbox_settings")
33
self.pack_start(self.vbox_settings, False, False, 0)
36
self.account = account or {}
38
self.window = dialog.dialog
39
has_secret_key = False
40
if self.account.has_key("id"):
41
has_secret_key = get_from_keyring(self.account['id'],
42
'secret_token') is not None
45
if self.account.has_key("access_token") and self.account.has_key("secret_token") and self.account.has_key("username") and has_secret_key and not self.dialog.condition:
46
self.ui.get_object("hbox_foursquare_auth").hide()
47
self.ui.get_object("foursquare_auth_done_label").set_label(_("%s has been authorized by Foursquare") % self.account["username"])
48
self.ui.get_object("hbox_foursquare_auth_done").show()
50
self.ui.get_object("hbox_foursquare_auth_done").hide()
52
self.dialog.ui.get_object('vbox_create').hide()
54
self.ui.get_object("hbox_foursquare_auth_done").hide()
56
self.dialog.ui.get_object("vbox_create").hide()
58
def console_message_cb (self, *args):
61
def on_foursquare_auth_clicked(self, widget, data=None):
62
self.winsize = self.window.get_size()
64
web = WebKit.WebView()
65
web.get_settings().set_property("enable-plugins", False)
66
web.get_settings().set_property("enable-developer-extras", False)
67
web.connect("console-message", self.console_message_cb)
68
web.load_html_string(_("<p>Please wait...</p>"), "file:///")
70
url = 'https://foursquare.com/oauth2/authenticate?client_id=' + CLIENT_ID + '&response_type=code&display=touch&redirect_uri='+ REDIRECT_URI
73
web.connect("title-changed", self.on_foursquare_auth_title_change)
75
self.scroll = Gtk.ScrolledWindow()
76
self.scroll.set_size_request(500, 400)
79
self.pack_start(self.scroll, True, True, 0)
82
self.ui.get_object("vbox1").hide()
83
self.ui.get_object("vbox_advanced").hide()
84
self.dialog.infobar.set_message_type(Gtk.MessageType.INFO)
86
def on_foursquare_auth_title_change(self, web=None, title=None, data=None):
88
if hasattr(self.dialog, "infobar_content_area"):
89
for child in self.dialog.infobar_content_area.get_children(): child.destroy()
90
self.dialog.infobar_content_area = self.dialog.infobar.get_content_area()
91
self.dialog.infobar_content_area.show()
93
if title.get_title() == "Success":
94
message_label = Gtk.Label(_("Verifying"))
95
message_label.set_use_markup(True)
96
message_label.set_ellipsize(Pango.EllipsizeMode.END)
97
self.dialog.infobar_content_area.add(message_label)
98
self.dialog.infobar.show_all()
101
#Get the code from the callback uri (it's formatted as http://gwibber.com/0/auth.html/?code=CODE)
102
url = web.get_main_frame().get_uri()
103
data = urlparse.parse_qs(url.split("?", 1)[1])
104
self.code = data["code"][0]
106
self.ui.get_object("vbox1").show()
107
self.ui.get_object("vbox_advanced").show()
109
#Exchange the code for an access token
110
url = "https://foursquare.com/oauth2/access_token?client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&grant_type=authorization_code&redirect_uri=" + REDIRECT_URI + "&code=" + self.code
111
self.access_token = json.load(urllib2.urlopen(url))['access_token']
112
self.account["access_token"] = self.access_token
113
self.account["secret_token"] = self.access_token
115
#Make a request with our new token for the user's own data
116
url = "https://api.foursquare.com/v2/users/self?oauth_token=" + self.access_token
117
data = json.load(urllib2.urlopen(url))
119
if data["response"]["user"].has_key("firstName"):
120
fullname += data["response"]["user"]["firstName"] + " "
121
if data["response"]["user"].has_key("lastName"):
122
fullname += data["response"]["user"]["lastName"]
123
self.account["username"] = fullname
124
self.account["user_id"] = data["response"]["user"]["id"]
126
if isinstance(data, dict):
127
data = data["response"]["user"]
128
if data.has_key("id"):
129
saved = self.dialog.on_edit_account_save()
132
self.dialog.infobar.set_message_type(Gtk.MessageType.ERROR)
133
message_label.set_text(_("Authorization failed. Please try again."))
136
self.dialog.infobar.set_message_type(Gtk.MessageType.ERROR)
137
message_label.set_text(_("Authorization failed. Please try again."))
140
message_label.set_text(_("Successful"))
141
self.dialog.infobar.set_message_type(Gtk.MessageType.INFO)
142
#self.dialog.infobar.hide()
144
self.ui.get_object("hbox_foursquare_auth").hide()
145
self.ui.get_object("foursquare_auth_done_label").set_label(_("%s has been authorized by Foursquare") % str(self.account["username"]))
146
self.ui.get_object("hbox_foursquare_auth_done").show()
147
if self.dialog.ui and self.account.has_key("id") and not saved:
148
self.dialog.ui.get_object("vbox_save").show()
149
elif self.dialog.ui and not saved:
150
self.dialog.ui.get_object("vbox_create").show()
152
self.window.resize(*self.winsize)
154
if title.get_title() == "Failure":
155
self.dialog.infobar.set_message_type(Gtk.MessageType.ERROR)
156
message_label = Gtk.Label(_("Authorization failed. Please try again."))
157
message_label.set_use_markup(True)
158
message_label.set_ellipsize(Pango.EllipsizeMode.END)
159
self.dialog.infobar_content_area.add(message_label)
160
self.dialog.infobar.show_all()
162
self.ui.get_object("vbox1").show()
163
self.ui.get_object("vbox_advanced").show()
164
self.scroll.destroy()
165
self.window.resize(*self.winsize)