Skip to content

Instantly share code, notes, and snippets.

@vinitkumar
Created February 26, 2014 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinitkumar/9234655 to your computer and use it in GitHub Desktop.
Save vinitkumar/9234655 to your computer and use it in GitHub Desktop.
Object literal based design for chrome extension
var getAccess = {
config : {
client_id: '255350354640375',
client_secret: '0206d7cd66962dd59d90472d810c3464',
successURL: 'http://gotchacode.github.io'
},
init: function (config) {
token = getAccess.getTempToken();
getAccess.getPermToken(token);
},
getTempToken: function() {
var accessToken;
if (!accessToken) {
chrome.tabs.getAllInWindow(null, function (tabs) {
_.each(tabs, function (tab) {
if (tab.url.indexOf(getAccess.config.successURL) === 0) {
var token = tab.url.split('#')[1].split('&')[0];
return token;
}
});
chrome.tabs.onUpdated.removeListener(getAccess.getTempToken);
return;
});
}
},
getPermToken: function (token) {
$.ajax({
type: "POST",
url: "https://graph.facebook.com/oauth/access_token",
data: {
grant_type: "fb_exchange_token",
client_id: getAccess.config.client_id,
client_secret: getAccess.config.client_secret,
fb_exchange_token: token
},
success: function(url) {
permToken = url.split("&")[0];
if (permToken) {
return permToken;
}
},
error: function() {
console.log('debug like a pro!');
}
});
}
};
// Initialise the getAccess to get Access tokens from facebook
getAccess.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment