Skip to content

Instantly share code, notes, and snippets.

@gashtio
Created February 22, 2013 08:34
Show Gist options
  • Save gashtio/5011776 to your computer and use it in GitHub Desktop.
Save gashtio/5011776 to your computer and use it in GitHub Desktop.
The whole JavaScript file for the Unity3D Facebook integration with Coherent UI sample
var appID = '344764665598630';
var appURL = 'http://www.coherent-labs.com/sample.html';
var userID;
var userToken;
function SetPortrait(fbAvatar) {
var url = fbAvatar.picture.data.url;
$('#fbButton').css("background-image", "url(" + url + ")");
}
function ToBlob(data) {
return new Blob([new Int8Array(data)]);
}
function PostOnWall(message) {
var errorString = "An error occured when trying to post to FaceBook! Make sure you're logged in by clicking the icon in the top left corner.";
if (typeof(FB) == 'undefined') {
alert(errorString);
return;
}
FB.api('/me/feed', 'post', {message : message}, function (response) {
if (!response || response.error) {
console.error(errorString);
} else {
console.log("Post ID: " + response.id);
}
});
}
function UploadImageOnFacebook(data, message) {
var formData = new FormData;
formData.append('message', message);
formData.append('source', ToBlob(data));
$.ajax({
url: 'https://graph.facebook.com/' + userID + '/photos?access_token=' + userToken,
type: 'POST',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
}).success(function () {
console.log(arguments);
}).error(function () {
console.error(arguments);
});
}
function parseAuthorization(url) {
var v = url.split('#')[1].split('&');
var n = v.length;
var r = {}
for (var i = 0; i != n; ++i) {
var c = v[i].split('=');
r[c[0]] = decodeURIComponent(c[1]);
}
return r;
}
if (window.location.hash.length == 0)
{
$(function () {
var button = $('<div id="fbButton" class="fbLogoDisabled"></div>');
button.click(function () {
var path = 'https://www.facebook.com/dialog/oauth?';
var queryParams = {
client_id: appID,
redirect_uri: appURL,
response_type: 'token,signed_request,code',
scope: 'user_photos,publish_stream'
};
var url = path + $.param(queryParams);
// redirect the view to the facebook authorization dialog
window.location.href = url;
});
$('body').append(button);
});
}
else
{
(function(d){
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 = "http://connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
$(function () {
var button = $('<div id="fbButton" class="fbLogoEnabled"></div>');
$('body').append(button);
});
}
// Init the SDK upon load
window.fbAsyncInit = function() {
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function(response) {
if (response.authResponse) {
// user has auth'd your app and is logged into Facebook
// request users' first name and profile picture
FB.api('/me?fields=picture,first_name', function(me){
$('#name').html(me.first_name);
SetPortrait(me);
userID = me.id;
});
}
})
// parse the authorization response details from the URL
var parsed = parseAuthorization(window.location.href);
userToken = parsed.access_token;
// convert to FB authResponse
var auth = {
accessToken : userToken,
signedRequest : parsed.signed_request,
expiresIn : parsed.expires_in,
code: parsed.code
};
FB.init({
appId : appID,
authResponse: auth,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : false // parse XFBML
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment