Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fields, auth_response, session restore, init with dict #18

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# OpenFB #

Modified OpenFB is a fork of OpenFB.

OpenFB is a micro-library that lets you integrate your JavaScript applications with Facebook. It works for both browser-based and Cordova/PhoneGap apps.

OpenFB has no dependency: You don't need the Facebook plugin when running in Cordova. You also don't need the Facebook SDK.
Expand Down Expand Up @@ -29,6 +31,12 @@ Post on the user's feed:
error: errorHandler
});

Get the user ID:

openFB.api({path: '/me?fields:id', success: successHandler, error: errorHandler});



The approach used in OpenFB (plain OAuth + direct requests to Graph API endpoints) is simple and lightweight, but it is definitely not perfect.

Pros:
Expand Down
1 change: 1 addition & 0 deletions contributors
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fernando Junior (@fernandojunior)
28 changes: 25 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<hr/>

<p>Name: <span id="userName"></span></p>
<p>ID: <span id="userID"></span></p>
<button class="btn btn-block" onclick="getInfo()">Get My Info</button>
<button class="btn btn-block" onclick="getUserId()">Get My ID</button>
<hr/>

<textarea id="Message" placeholder="What's on your mind?" rows="5"></textarea>
Expand All @@ -26,11 +28,15 @@
<script src="openfb.js"></script>

<script>

openFB.init('YOUR_FB_APP_ID'); // Defaults to sessionStorage for storing the Facebook token

openFB.init({
app_id: "YOUR_FB_APP_ID",
//redirect_url: "http://localhost/oauthcallback.html" // oauth redirect url
// auth_response: {access_token: "", expires_in: "", user_id: ""}, // restore a session
}); // Defaults to sessionStorage for storing the Facebook token

// Uncomment the line below to store the Facebook token in localStorage instead of sessionStorage
// openFB.init('YOUR_FB_APP_ID', 'http://localhost/openfb/oauthcallback.html', window.localStorage);
// openFB.init({app_id: 'YOUR_FB_APP_ID', redirect_url: 'http://localhost/openfb/oauthcallback.html', store: window.localStorage});

function login() {
openFB.login('email',
Expand All @@ -51,6 +57,22 @@
},
error: errorHandler});
}

function getUserId(){

if (openFB.getAuthResponse())
console.log(JSON.stringify(openFB.getAuthResponse()));

openFB.api({
path: "/me?fields:id",
success: function(data){
console.log(JSON.stringify(data));
document.getElementById("userID").innerHTML += data.id;
},
error: errorHandler
});

}

function share() {
openFB.api({
Expand Down
74 changes: 64 additions & 10 deletions openfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,29 @@ var openFB = (function () {
* @param redirectURL - The OAuth redirect URL. Optional. If not provided, we use sensible defaults.
* @param store - The store used to save the Facebook token. Optional. If not provided, we use sessionStorage.
*/
function init(appId, redirectURL, store) {
fbAppId = appId;
if (redirectURL) oauthRedirectURL = redirectURL;
if (store) tokenStore = store;
function init(args) {
var app_id = args.app_id;
var redirect_url = args.redirect_url;
var store = args.store;
var auth_response = args.auth_response;

fbAppId = app_id;

if (redirect_url) {
oauthRedirectURL = redirect_url;
}

if (store) {
tokenStore = store;
}

// restore a authentication
if (auth_response){
tokenStore["fbtoken"] = auth_response["access_token"];
tokenStore["expires_in"] = auth_response["expires_in"];
tokenStore["user_id"] = auth_response["user_id"];
}

}

/**
Expand Down Expand Up @@ -78,6 +97,7 @@ var openFB = (function () {
deferredLogin.reject({error: 'user_cancelled', error_description: 'User cancelled login process', error_reason: "user_cancelled"});
loginWindow.removeEventListener('loadstop', loginWindowLoadStart);
loginWindow.removeEventListener('exit', loginWindowExit);
//loginWindow.close();
loginWindow = null;
console.log('done removing listeners');
}
Expand Down Expand Up @@ -128,15 +148,29 @@ var openFB = (function () {
*/
function oauthCallback(url) {
// Parse the OAuth data received from Facebook
var queryString,
obj;
var queryString, obj;

loginProcessed = true;
if (url.indexOf("access_token=") > 0) {
obj = {};
queryString = url.substr(url.indexOf('#') + 1);
obj = parseQueryString(queryString);
tokenStore['fbtoken'] = obj['access_token'];
if (loginSuccessHandler) loginSuccessHandler();
obj.auth_response = parseQueryString(queryString);
tokenStore['fbtoken'] = obj.auth_response['access_token'];
tokenStore['expires_in'] = obj.auth_response['expires_in'];

api({
path: '/me?fields=id',
success: function(data) {
obj.auth_response.user_id = data.id;
tokenStore['user_id'] = obj.auth_response.user_id;

if (loginSuccessHandler) loginSuccessHandler(obj);
},
error: function(error){
if (loginErrorHandler) loginErrorHandler(error);
}
});

} else if (url.indexOf("error=") > 0) {
queryString = url.substring(url.indexOf('?') + 1, url.indexOf('#'));
obj = parseQueryString(queryString);
Expand All @@ -145,6 +179,23 @@ var openFB = (function () {
if (loginErrorHandler) loginErrorHandler();
}
}

/**
* Get the auth response of the current user logged in. Similar to FB api.
**/
function getAuthResponse(){

var auth_response = {};

if (tokenStore['fbtoken']){
auth_response.access_token = tokenStore['fbtoken'];
auth_response.expires_in = tokenStore['expires_in'];
auth_response.user_id = tokenStore['user_id'];
}

return auth_response;

}

/**
* Application-level logout: we simply discard the token.
Expand All @@ -171,7 +222,9 @@ var openFB = (function () {

params['access_token'] = tokenStore['fbtoken'];

url = 'https://graph.facebook.com' + obj.path + '?' + toQueryString(params);
url = 'https://graph.facebook.com' + obj.path;
url += (obj.path.indexOf('?') > -1) ? '&' : '?';
url += toQueryString(params);

xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
Expand Down Expand Up @@ -231,6 +284,7 @@ var openFB = (function () {
login: login,
logout: logout,
revokePermissions: revokePermissions,
getAuthResponse: getAuthResponse,
api: api,
oauthCallback: oauthCallback
}
Expand Down