Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

facebook plugin

  • Loading branch information...
commit 36c5c4ae8334f99bb87fa473efcd10eb695fa2e0 1 parent b87c85c
sgrebnov authored Jesse MacFadyen committed
View
129 WindowsPhone/Facebook/FBConnect.js
@@ -0,0 +1,129 @@
+/* MIT licensed */
+// (c) 2010 Jesse MacFadyen, Nitobi
+// (c) 2011 Sergey Grebnov
+// Contributions, advice from :
+// http://www.pushittolive.com/post/1239874936/facebook-login-on-iphone-phonegap
+
+/**
+* FBConnect implements user authentication logic and session information store
+*/
+
+function FBConnect(client_id, redirect_uri, display) {
+
+ this.client_id = client_id;
+ this.redirect_uri = redirect_uri;
+ this.display = display;
+
+ this.resetSession();
+
+ if (window.plugins.childBrowser == null) {
+ ChildBrowser.install();
+ }
+
+}
+
+/**
+* User login
+*/
+FBConnect.prototype.connect = function (scope) {
+
+ var authorize_url = "https://graph.facebook.com/oauth/authorize?";
+ authorize_url += "client_id=" + this.client_id;
+ authorize_url += "&redirect_uri=" + this.redirect_uri;
+ authorize_url += "&display=" + (this.display ? this.display : "touch");
+ authorize_url += "&type=user_agent";
+
+ // extended permissions http://developers.facebook.com/docs/reference/api/permissions/
+ if (scope) {
+ authorize_url += "&scope=" + scope;
+ }
+
+ window.plugins.childBrowser.showWebPage(authorize_url);
+ var self = this;
+ window.plugins.childBrowser.onLocationChange = function (loc) { self.onLoginLocationChange(loc); };
+}
+
+FBConnect.prototype.onLoginLocationChange = function (newLoc) {
+ if (newLoc.indexOf(this.redirect_uri) == 0) {
+ var result = unescape(newLoc).split("#")[1];
+ result = unescape(result);
+
+ // TODO: Error Check
+ this.session.access_token = result.split("&")[0].split("=")[1];
+ var expiresIn = parseInt(result.split("&")[1].split("=")[1]);
+ this.session.expires = new Date().valueOf() + expiresIn * 1000;
+ this.status = "connected";
+
+ window.plugins.childBrowser.close();
+ this.onConnect(this);
+
+ }
+}
+
+/**
+* User logout
+*/
+FBConnect.prototype.logout = function () {
+ var authorize_url = "https://www.facebook.com/logout.php?";
+ authorize_url += "&next=" + this.redirect_uri;
+ authorize_url += "&access_token=" + this.session.access_token;
+ console.log("logout url: " + authorize_url);
+ window.plugins.childBrowser.showWebPage(authorize_url);
+ var self = this;
+ window.plugins.childBrowser.onLocationChange = function (loc) {
+ console.log("onLogout");
+ window.plugins.childBrowser.close();
+ self.resetSession();
+ self.status = "notConnected";
+ self.onDisconnect(this);
+ };
+}
+
+/**
+* Example method - returns your friends
+*/
+FBConnect.prototype.getFriends = function () {
+ var url = "https://graph.facebook.com/me/friends?access_token=" + this.session.access_token;
+ var req = new XMLHttpRequest();
+
+ req.open("get", url, true);
+ req.send(null);
+ req.onerror = function () { alert("Error"); };
+ return req;
+}
+
+// Note: this plugin does NOT install itself, call this method some time after deviceready to install it
+// it will be returned, and also available globally from window.plugins.fbConnect
+FBConnect.install = function (client_id, redirect_uri, display) {
+ if (!window.plugins) {
+ window.plugins = {};
+ }
+ window.plugins.fbConnect = new FBConnect(client_id, redirect_uri, display);
+
+ return window.plugins.fbConnect;
+}
+
+/**
+* Session management functionality
+*/
+FBConnect.prototype.resetSession = function () {
+ this.status = "unknown";
+ this.session = {};
+ this.session.access_token = null;
+ this.session.expires = 0;
+ this.session.secret = null;
+ this.session.session_key = null;
+ this.session.sig = null;
+ this.session.uid = null;
+}
+
+FBConnect.prototype.restoreLastSession = function () {
+ var session = JSON.parse(localStorage.getItem('pg_fb_session'));
+ if (session) {
+ this.session = session;
+ }
+}
+
+FBConnect.prototype.saveSession = function () {
+ localStorage.setItem('pg_fb_session', JSON.stringify(this.session));
+}
View
169 WindowsPhone/Facebook/facebook.html
@@ -0,0 +1,169 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+ <head>
+ <!-- Change this if you want to allow scaling -->
+ <meta name="viewport" content="width=default-width; user-scalable=no" />
+
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+
+ <title>FBConnectBrowser</title>
+
+ <script type="text/javascript">
+ // provide our own console if it does not exist, huge dev aid!
+ if (typeof window.console == "undefined") {
+ window.console = { log: function (str) { window.external.Notify(str); } };
+ }
+
+ // output any errors to console log, created above.
+ window.onerror = function (e) {
+ console.log("window.onerror ::" + JSON.stringify(e));
+ };
+
+ console.log("Installed console ! ");
+ </script>
+
+ <!-- iPad/iPhone specific css below, add after your main css >
+ <link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
+ <link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />
+ -->
+
+ <script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
+ <script type="text/javascript" charset="utf-8" src="ChildBrowser.js"></script>
+ <script type="text/javascript" charset="utf-8" src="FBConnect.js"></script>
+
+
+ <style>
+
+ body
+ {
+ background-color:#3B5998;
+ color:#FFF;
+ font-family: Helvetica, Verdana;
+ margin-bottom:20px;
+ }
+
+ #loading
+ {
+ z-index:1;
+ width:100%;
+ top:200px;
+ position:absolute;
+ text-align:center;
+ }
+
+ #gbWrap
+ {
+ overflow:hidden;
+ position:absolute;
+ top:60px;
+ bottom:0px;
+ }
+
+ .view
+ {
+ width:320px;
+ float:left;
+ clear:none;
+
+ }
+
+
+ #fbFriends li
+ {
+ list-style:none;
+
+ height:56px;
+ background:-webkit-gradient(
+ linear,
+ left top,
+ left bottom,
+ color-stop(0, rgba(0,0,0,0.1)),
+ color-stop(1, rgba(128,128,128,0.1))
+ )
+ }
+
+ #fbFriends li:active
+ {
+ background-color:rgba(255,255,255,0.5);
+ }
+
+ #fbFriends li img
+ {
+ margin:2px 12px;
+ min-height:48px;
+ min-width:48px;s
+ }
+
+ #fbFriends li span
+ {
+ position:relative;
+ top:-12px;
+ }
+
+ #details
+ {
+ position:absolute;
+ left:320px;
+ height:100%;
+ }
+
+
+ </style>
+
+ <script type="text/javascript" charset="utf-8">
+
+ var client_id = "311961255484993";
+ var redir_url = "http://www.facebook.com/connect/login_success.html";
+ var friendsMap = {};
+
+ function onBodyLoad() {
+ document.addEventListener("deviceready", onDeviceReady, false);
+ }
+
+ /* When this function is called, PhoneGap has been initialized and is ready to roll */
+ function onDeviceReady() {
+ // do your thing!
+ var fb = FBConnect.install(client_id, redir_url, "touch");
+ fb.connect('email, read_stream, read_friendlists');
+ fb.onConnect = onFBConnected;
+ }
+
+ function onFBConnected() {
+ document.getElementById("loading").innerHTML = "Connected! Getting your friends ...";
+ var req = window.plugins.fbConnect.getFriends();
+ req.onload = onGotFriends;
+ }
+
+ function onGotFriends(evt) {
+ document.getElementById("loading").style.visibility = "hidden";
+
+ var json = JSON.parse(evt.target.responseText);
+
+
+ var listItems = [];
+ var template = "<li id='FBID' onclick='showUser(\"FBID\")'><img src='https://graph.facebook.com/FBID/picture'/><span>NAME</span></li>";
+ for (var n = 0, len = json.data.length; n < len; n++) {
+ var friend = json.data[n];
+ friendsMap[friend.id] = friend;
+ listItems.push(template.replace(/FBID/g, friend.id).replace(/NAME/g, friend.name));
+ }
+ document.getElementById("fbFriends").innerHTML = listItems.join("");
+ }
+
+ function showUser(id) {
+ // TODO: Do something with user id ...
+ alert(friendsMap[id].name);
+ }
+
+ </script>
+ </head>
+ <body onload="onBodyLoad()">
+ <h3 id="loading">Loading ...</h3>
+
+ <ul id="fbFriends">
+ <!-- Placeholder for later -->
+ </ul>
+
+
+ </body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.