From 0b0df57042a225023f458d22941f3349ca707450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20R=2E=20Ruiz?= Date: Fri, 4 Nov 2011 19:19:40 +0000 Subject: [PATCH] Initial commit --- FacebookAuth.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE | 26 +++++++++++++++ README | 8 +++++ 3 files changed, 118 insertions(+) create mode 100644 FacebookAuth.py create mode 100644 LICENSE create mode 100644 README diff --git a/FacebookAuth.py b/FacebookAuth.py new file mode 100644 index 0000000..55806d4 --- /dev/null +++ b/FacebookAuth.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +import pygtk +import gtk +import webkit +import urllib +import urlparse + +FB_TOKEN_FILE = 'access_token.txt' + +class Browser: + """ Creates a web browser using GTK+ and WebKit to authorize a + desktop application in Facebook. It uses OAuth 2.0. + Requires the Facebook's Application ID. The token is then + saved to FB_TOKEN_FILE. + """ + + def __init__(self, app_key, scope='offline_access'): + """ Constructor. Creates the GTK+ app and adds the WebKit widget + @param app_key Application key ID (Public). + + @param scope A string list of permissions to ask for. More at + http://developers.facebook.com/docs/reference/api/permissions/ + """ + self.token = '' + self.token_expire = '' + self.scope = scope + # Creates the GTK+ app + self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) + self.scrolled_window = gtk.ScrolledWindow() + # Creates a WebKit view + self.web_view = webkit.WebView() + self.scrolled_window.add(self.web_view) + self.window.add(self.scrolled_window) + # Connects events + self.window.connect('destroy', self._destroy_event_cb) # Close window + self.web_view.connect('load-committed', self._load_committed_cb) # Load page + self.window.set_default_size(1024, 800) + # Loads the Facebook OAuth page + self.web_view.load_uri( + 'https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s&response_type=token&scope=%s' % (urllib.quote(app_key), urllib.quote('https://www.facebook.com/connect/login_success.html'), urllib.quote(self.scope)) + ) + + def _load_committed_cb(self, web_view, frame): + """ Callback. The page is about to be loaded. This event is captured + to intercept the OAuth 2.0 redirection, which includes the + access token. + + @param web_view A reference to the current WebKitWebView. + + @param frame A reference to the main WebKitWebFrame. + """ + # Gets the current URL to check whether is the one of the redirection + uri = frame.get_uri() + parse = urlparse.urlparse(uri) + if (hasattr(parse, 'netloc') and hasattr(parse, 'path') and + hasattr(parse, 'fragment') and parse.netloc == 'www.facebook.com' and + parse.path == '/connect/login_success.html' and parse.fragment): + # Get token from URL + params = urlparse.parse_qs(parse.fragment) + self.token = params['access_token'][0] + self.token_expire = params['expires_in'][0] # Should be equal to 0, don't expire + # Save token to file + token_file = open(FB_TOKEN_FILE, 'w') + token_file.write(self.token) + token_file.close() + print "Authentication done. Access token available at %s" % (FB_TOKEN_FILE) + gtk.main_quit() # Finish + + def _destroy_event_cb(self, widget): + """ Callback for close window. Closes the application. """ + return gtk.main_quit() + + def authorize(self): + """ Runs the app. """ + self.window.show_all() + gtk.main() + +if (__name__ == '__main__'): + # Creates the browser + browser = Browser(app_key='XXXXXXXXXXX', scope='offline_access,read_stream') + # Launch browser window + browser.authorize() + # Token available? + print "Token: %s" % (browser.token) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..65b2d49 --- /dev/null +++ b/LICENSE @@ -0,0 +1,26 @@ +Copyright (c) 2011, Víctor R. Ruiz +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. Neither the name of the author nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN +NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README b/README new file mode 100644 index 0000000..f9ce472 --- /dev/null +++ b/README @@ -0,0 +1,8 @@ +facebookAuth.py +=============== + +Creates a web browser using GTK+ and WebKit to authorize a desktop +application in Facebook. It uses OAuth 2.0. Requires the Facebook's +Application ID. The token is then saved to FB_TOKEN_FILE. + +(C) 2011 Víctor R. Ruiz