Created
November 12, 2012 05:54
-
-
Save fmoo/4057731 to your computer and use it in GitHub Desktop.
Getting a Facebook Access Token in python via QWebView + Facebook Javascript SDK
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
from PySide.QtCore import Slot | |
from PySide.QtGui import QApplication, QMainWindow, QDialog, QVBoxLayout | |
from PySide.QtWebKit import QWebView, QWebPage, QWebSettings | |
import sys | |
from functools import partial | |
from argparse import ArgumentParser | |
ap = ArgumentParser() | |
ap.add_argument('app_id', type=int) | |
ns = ap.parse_args() | |
app = QApplication(sys.argv) | |
win = QMainWindow() | |
win.application = app | |
webview = QWebView(win) | |
class MyWebPage(QWebPage): | |
def __init__(self, *args, **kwargs): | |
QWebPage.__init__(self, *args, **kwargs) | |
self.mainFrame().addToJavaScriptWindowObject('qpage', self) | |
def javaScriptConsoleMessage(self, message, lineNumber, sourceID): | |
print "JS (%s:%d)> %s" % (sourceID, lineNumber, message) | |
def createWindow(self, type): | |
view = QWebView(self.parent()) | |
view.show() | |
view.page().windowCloseRequested.connect( | |
partial(self.onWindowCloseRequested, view)) | |
return view.page() | |
print "creating window of type", type | |
page_win = QDialog(win) | |
child_view = QWebView(page_win) | |
layout = QVBoxLayout() | |
page_win.setLayout(layout) | |
layout.addWidget(child_view) | |
page_win.setModal(True) | |
page_win.show() | |
return child_view.page() | |
@Slot(str) | |
def setAccessToken(self, access_token): | |
print "GOT ACCESS TOKEN: ", access_token | |
def onWindowCloseRequested(self, view): | |
view.hide() | |
webpage = MyWebPage(webview) | |
webpage.settings().setAttribute(QWebSettings.JavascriptEnabled, True) | |
webpage.settings().setAttribute(QWebSettings.JavascriptCanOpenWindows, True) | |
webview.setPage(webpage) | |
win.setCentralWidget(webview) | |
win.show() | |
html = """ | |
<!DOCTYPE HTML> | |
<html> | |
<head> | |
</head> | |
<body> | |
<div id="fb-root" /> | |
<div id="message" />Connecting to Facebook...</div> | |
<script src="//connect.facebook.net/en_US/all.js"></script> | |
</body> | |
</html> | |
""" | |
INIT = """ | |
FB.init({ | |
appId : '%s', | |
status : true, // check the login status upon init? | |
cookie : false, // set sessions cookies to allow your server to access the session? | |
xfbml : false // parse XFBML tags on this page? | |
}); | |
FB.login(function(response) { | |
if (response.authResponse) { | |
window.qpage.setAccessToken(response.authResponse.accessToken); | |
console.log('Welcome! Fetching your information.... '); | |
FB.api('/me', function(response) { | |
console.log('Good to see you, ' + response.name + '.'); | |
}); | |
document.getElementById('message').textContent = 'Success!'; | |
} else { | |
console.log('User cancelled login or did not fully authorize.'); | |
document.getElementById('message').textContent = 'failure!'; | |
} | |
}); | |
""" % (ns.app_id) | |
inited = False | |
def onload(ok): | |
global inited | |
if not inited: | |
inited = True | |
webpage.mainFrame().evaluateJavaScript(INIT) | |
webpage.loadFinished.connect(onload) | |
webpage.mainFrame().setHtml(html, 'http://localhost:50000/') | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment