Skip to content

Instantly share code, notes, and snippets.

@astrotars
Created January 31, 2013 17:43
Show Gist options
  • Select an option

  • Save astrotars/4684712 to your computer and use it in GitHub Desktop.

Select an option

Save astrotars/4684712 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Facebook | Manage Pages</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
var GLOBAL = {
FACEBOOK_APP_ID: 525835520781132
}
</script>
<style type="text/css">
body {
font-family: sans-serif;
font-weight: 200;
}
</style>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : GLOBAL.FACEBOOK_APP_ID, // facebook app id
channelUrl : '//localhost/channel.php', // channel File for x-domain communication
status : true, // check the login status upon init
cookie : true, // set sessions cookies to allow your server to access the session?
xfbml : true // parse XFBML tags on this page
});
// additional initialization code such as adding event listeners goes here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and authenticated
$('#status').html('Logged into Facebook and authenticated.');
// get admin pages
getFbPages();
} else if (response.status === 'not_authorized') {
// the user is logged in to facebook, but has not authenticated the app
$('#status').html('Logged into Facebook and not authenticated. <button id="auth">Authenticate</button>');
} else {
// the user isn't logged in to Facebook.
$('#status').html('Logged out of Facebook.');
}
});
// on click of the authenticate button
$('body').on('click', 'button#auth', function() {
FB.login(function(response) {
// on success
if (response.authResponse) {
// get admin pages
getFbPages();
// on error
} else {
$('#status').html('User canceled login or did not fully authorize.');
}
}, {scope: 'manage_pages,publish_stream'});
});
// get all pages that the user is an admin of
function getFbPages(fbUid, accessToken) {
FB.api('/me/accounts', function(response) {
var pages = response.data;
var ln = pages.length;
// loop through pages
for (i = 0; i < ln; i++) {
// populate dropdown
$('#pages select').append('<option value="' + pages[i].id + '" data="' + pages[i].access_token + '">' + pages[i].name + '</option>');
}
// display dropdown
$('#pages').fadeIn();
});
}
function verifyInstall(pageId, pageAccessToken) {
// check if application tab is installed on page
FB.api('/' + pageId + '/tabs/' + GLOBAL.FACEBOOK_APP_ID, { access_token: pageAccessToken }, function(response) {
console.log(response);
var tab = response.data;
if (response.data.length > 0) {
console.log('TAB INFO', tab[0]);
$('#installed').html('Application tab is installed. <a href="' + tab[0].link + '" target="_blank">Go to App</a>');
}
// display installed status div
$('#installed').hide().fadeIn();
});
}
function installAppTab(pageId, pageAccessToken) {
FB.api('/' + pageId + '/tabs', 'POST', {
app_id: GLOBAL.FACEBOOK_APP_ID,
access_token: pageAccessToken
}, function(response) {
console.log('INSTALL STATUS', response);
verifyInstall(pageId, pageAccessToken);
});
}
// on save of page
$('body').on('click', 'button#save', function() {
var selected = $('#pages select option:selected');
var pageName = selected.text();
var pageId = selected.val()
var pageAccessToken = selected.attr('data');
verifyInstall(pageId, pageAccessToken);
});
// on click of install page tab
$('body').on('click', 'button#install', function() {
var selected = $('#pages select option:selected');
var pageId = selected.val()
var pageAccessToken = selected.attr('data');
// install page tab
installAppTab(pageId, pageAccessToken);
});
};
// Load the SDK's source Asynchronously
// Note that the debug version is being actively developed and might
// contain some type checks that are overly strict.
// Please report such bugs using the bugs tool.
(function(d, debug) {
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ true));
</script>
<div id="status"></div>
<div id="pages" style="display: none;">
Select Page:
<select>
<option value="">SELECT PAGE</option>
</select>
<button id="save">Save</button>
</div>
<div id="installed" style="display: none;">
Application tab is not installed. <button id="install">Install Page Tab</button>
</div>
</body>
<html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment