I had a lot of trouble figuring out how to make server side calls once a user logged in with Facebook Connect. The key is that the Facebook Connect javascript sets cookies on the client once there's a successful login. You use the values of these cookies to perform API calls on the server.
The confusing part was looking at the PHP sample they released. Their server side API automatically takes care of reading these cookie values and setting up an API object that's ready to make requests on behalf of the logged in user.
Here's an example using the Facebook Toolkit on the server after the user has logged in with Facebook Connect.
Server code:
API api = new API();
api.ApplicationKey = Utility.ApiKey();
api.SessionKey = Utility.SessionKey();
api.Secret = Utility.SecretKey();
api.uid = Utility.GetUserID();
facebook.Schema.user user = api.users.getInfo();
string fullName = user.first_name + " " + user.last_name;
foreach (facebook.Schema.user friend in api.friends.getUserObjects())
{
// do something with the friend
}
Utility.cs
public static class Utility
{
public static string ApiKey()
{
return ConfigurationManager.AppSettings["Facebook.API_Key"];
}
public static string SecretKey()
{
return ConfigurationManager.AppSettings["Facebook.Secret_Key"];
}
public static string SessionKey()
{
return GetFacebookCookie("session_key");
}
public static int GetUserID()
{
return int.Parse(GetFacebookCookie("user"));
}
private static string GetFacebookCookie(string name)
{
if (HttpContext.Current == null)
throw new ApplicationException("HttpContext cannot be null.");
string fullName = ApiKey() + "_" + name;
if (HttpContext.Current.Request.Cookies[fullName] == null)
throw new ApplicationException("Could not find facebook cookie named " + fullName);
return HttpContext.Current.Request.Cookies[fullName].Value;
}
}
It's definitely possible without a fb:login-button
. In fact, if you're building an iframe app, then you probably don't need an fb:login-button
at all. All you need is to load the Facebook Connect libraries, and then make the initialize call, like this (using your own API key of course):
<script src="http://static.ak.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
<script type="text/javascript">
FB.init("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "/xd_receiver.htm");
window.addEvent('domready',function(){
FB_RequireFeatures(["CanvasUtil"], function(){
FB.XdComm.Server.init("/xd_receiver.htm");
});
});
</script>
Note that I've got a bit of MooTools in there to make sure the RequireFeatures
call happens after the DOM is ready. You can stick to the examples in the initializing docs if you want.
Anyway, once you've loaded and initialized the FB Connect libraries, you should have full access to the API methods that the JS library provides, and you can simply do this:
var user_message_prompt = "Write something here!";
var user_message = {value: "Default message"};
if (FB.Connect) {
FB.Connect.showFeedDialog(templateBundle, templateData, null,null,null,FB.RequireConnect.require,null,user_message_prompt,user_message);
}
Best Answer
The FBDTK 2.0 is quite out of date already due to the the changes facebook have made to their apis. I've moved away from using it in my ASP.NET applications and gone with a pure javascript and manual XFBML approach which works very well with facebook connect and ASP.NET MVC.
You can't publish one line stories using connect at the moment. You need to pop up a feed dialog to allow this (it's not too intrusive though - it works quite well). This video explains it nicely:
http://www.facebook.com/video/video.php?v=636853997423
and you might want to look at the other videos here - very useful:
http://wiki.developers.facebook.com/index.php/Facebook_Connect_Video_Tutorials
basically though, to publish a feed story via facebook connect you need to do something like this:
as detailed here:
http://wiki.developers.facebook.com/index.php/Publishing_Feed_Stories_to_Facebook