Skip to content

Instantly share code, notes, and snippets.

@pc035860
Created July 3, 2012 03:01
Show Gist options
  • Save pc035860/3037267 to your computer and use it in GitHub Desktop.
Save pc035860/3037267 to your computer and use it in GitHub Desktop.
Facebook connect for Windows 8 metro style apps
/**
* Ask user for facebook connect & get the accesstoken
*
* @param {Object} options
* @param {Function} callback
* @param {Function} error_handler (optional)
**/
function requestFbToken (options, callback, error_handler) {
options = _extend({
appId: null,
redirectUri: 'https://www.facebook.com',
scope: [],
display: 'popup'
}, options);
if (options.appId === null) {
throw new Error('require app id');
}
if (typeof callback !== 'function') {
throw new Error('require callback function');
}
if (typeof error_handler !== 'undefined' &&
typeof error_handler !== 'function') {
throw new Error('error handler should be a function');
}
error_handler = error_handler || function (e) { console.log(e, e.stack); };
var auth_uri = 'https://www.facebook.com/dialog/oauth?' +
'client_id=' + options.appId +
'&redirect_uri=' + encodeURIComponent(options.redirectUri) +
'&scope=' + options.scope.join(',') +
'&display=' + options.display +
'&response_type=token',
callback_uri = options.redirectUri;
Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAsync(
Windows.Security.Authentication.Web.WebAuthenticationOptions.none,
new Windows.Foundation.Uri(auth_uri),
new Windows.Foundation.Uri(callback_uri)
)
.done(function (result) {
var m = result.responseData.match(/#access_token=(.+?)&/);
callback(m === null ? m : m[1]);
}, error_handler);
function _extend(d, s) {
for (var property in s) {
d[property] = s[property];
}
return d;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment