-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0b0df57
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Copyright (c) 2011, Víctor R. Ruiz <[email protected]> | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]> |