Skip to content

Instantly share code, notes, and snippets.

@johnschimmel
Created February 7, 2011 14:59
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 johnschimmel/814485 to your computer and use it in GitHub Desktop.
Save johnschimmel/814485 to your computer and use it in GitHub Desktop.
Titanium Facebook Graph Session
Titanium.Facebook.sessionForAccessToken = function(callback) {
var app_id = '<APP_ID>';
var app_secret = '<APP_SECRET>';
xhr.onerror = function(e)
{
Ti.API.info('Unable to exchange session for OAuth access_token ' + e.error);
callback(false);
};
xhr.setTimeout(10000);
xhr.onload = function(e)
{
var resultObj = JSON.parse(this.responseText); //should contain resultObj.access_token & resultObj.expires
if (resultObj[0].access_token != undefined) {
callback(resultObj); //returning the first item
} else {
callback(false);
}
};
// open the client
xhr.open('POST','https://graph.facebook.com/oauth/exchange_sessions');
// send the client_id, client_secret and comma delimited sessions (only 1 in this case)
xhr.send({client_id:app_id,client_secret:app_secret,sessions:Titanium.Facebook.session['session_key']});
};
Titanium.Facebook.addEventListener('login', function()
{
label.text = 'Logged In = ' + Titanium.Facebook.isLoggedIn();
if (Titanium.Facebook.isLoggedIn()) {
Titanium.Facebook.sessionForAccessToken(function(data) {
if (data[0].access_token != undefined) {
var access_token = data[0].access_token;
xhr.onerror = function(e) {
Ti.API.error('error on ' + e.error);
}
xhr.onload = function(e)
{
Ti.API.info(this.responseText);
};
// open the client
xhr.open('GET','https://graph.facebook.com/me?access_token=' +access_token);
xhr.send();
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment