Created
February 22, 2012 21:51
-
-
Save annasob/1887658 to your computer and use it in GitHub Desktop.
FB API integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(typeof A === 'undefined') { | |
A = {}; | |
} | |
A.FB = (function () { | |
var fbLoaded = false, | |
fbUserID, fbUserObject, fbUserToken; | |
return { | |
init: function () { | |
// Load the SDK Asynchronously | |
(function (d) { | |
var js, id = 'facebook-jssdk'; | |
if(d.getElementById(id)) { | |
return; | |
} | |
js = d.createElement('script'); | |
js.id = id; | |
js.async = true; | |
js.src = "//connect.facebook.net/en_US/all.js"; | |
d.getElementsByTagName('head')[0].appendChild(js); | |
}(document)); | |
}, | |
setLoaded: function (status) { | |
fbLoaded = status; | |
}, | |
setUserId: function (id) { | |
fbUserID = id; | |
}, | |
getUserId: function () { | |
return fbUserID; | |
}, | |
setUserObject: function (object) { | |
fbUserObject = object; | |
}, | |
getUserObject: function () { | |
return fbUserObject; | |
}, | |
setUserToken: function (token) { | |
fbUserToken = token; | |
}, | |
getUserToken: function () { | |
return fbUserToken; | |
}, | |
FBauthorize: function (url) { | |
// url needs redirect_uri and scope | |
if(A.mobile) { | |
window.location = "https://m.facebook.com/dialog/oauth?response_type=token&client_id=" + A.config.appId + "&" + url + "&scope=" + A.config.fb_permissions; | |
return; | |
} | |
window.location = "https://www.facebook.com/dialog/oauth?response_type=token&client_id=" + A.config.appId + "&" + url + "&scope=" + A.config.fb_permissions; | |
}, | |
FBGraphMe: function (token) { | |
this.setUserToken(token); | |
var script = document.createElement('script'); | |
script.src = "https://graph.facebook.com/me?callback=A.FB.FBGraphMeCallback&access_token=" + token; | |
document.body.appendChild(script); | |
}, | |
FBGraphMeCallback: function (data) { | |
if(data.error) { | |
return; | |
} | |
A.FB.setUserId(data.id); | |
A.FB.setUserObject(data); | |
}, | |
FBCheckLogin: function (perms) { | |
FB.getLoginStatus(function (response) { | |
if(response.authResponse) { | |
// logged in and connected user, someone you know | |
A.FB.setUserId(response.authResponse.userID); | |
} else { | |
// no user session available, someone you don't know | |
A.FB.FBLogIn(perms); | |
} | |
}); | |
}, | |
FBLogIn: function (perms) { | |
FB.login(function (response) { //redirect-url,page | |
if(response.authResponse) { | |
A.FB.setUserId(response.authResponse.userID); | |
} else { | |
A.FB.setUserId("unknown"); | |
} | |
}, { | |
scope: perms | |
}); | |
}, | |
FBUserQuery: function (method, queryparameter, fn) { | |
//http://developers.facebook.com/docs/reference/api/user/ | |
FB.api({ | |
method: method, | |
query: 'SELECT ' + queryparameter + ' FROM user WHERE uid = ' + fbUserID | |
}, function (response) { | |
A.FB.setUserObject(response[0]); | |
fn(response); | |
}); | |
}, | |
FBUserWallPost: function (name, link, picture, description, caption, message) { | |
FB.api('/me/feed', 'POST', { | |
method: 'stream.publish', | |
name: name, | |
link: link, | |
picture: picture, | |
description: description, | |
message: message, | |
caption: caption | |
}); | |
} | |
}; | |
})(); | |
window.fbAsyncInit = function () { | |
FB.init({ | |
appId: A.config.appId, | |
status: true, | |
// check login status | |
cookie: true, | |
// enable cookies to allow the server to access the session | |
oauth: true, | |
// enable OAuth 2.0 | |
xfbml: true // parse XFBML | |
}); | |
// mark as loaded | |
A.FB.setLoaded(true); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment