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;
}
}
try first login...
try
{
servicio.ApplicationKey = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
servicio.ConnectToFacebook(new List<Enums.ExtendedPermissions>() { Enums.ExtendedPermissions.read_stream, Enums.ExtendedPermissions.publish_stream });
label2.Text = "Conectado:";
label1.Text = servicio.Users.GetInfo().first_name + " " + servicio.Users.GetInfo().last_name;
pictureBox1.Image = servicio.Users.GetInfo().picture_small;
button2.Text = "Sesión Iniciada";
button2.Enabled = false;
checkBox1.Enabled = true;
numericUpDown1.Enabled = true;
groupBox3.Enabled = true;
groupBox4.Enabled = true;
}catch (FacebookException fe)
{
listBox1.Items.Add(fe.Message);
}
then you can publish.... like this..
try
{
var cancion = "lalalalala";
String cosa = servicio.Stream.Publish(cancion, null, new List<action_link>()
{
new action_link()
{
text="Visita SQLeros",
href="http://sqleros.com.ar/wps"
}
}, null, 0);
servicio.Stream.AddLike(cosa); //to add like (Y)
listBox1.Items.Add(cosa);
}
catch (FacebookException fe)
{
listBox1.Items.Add(fe.Message);
}
or this.. ñ_ñ
try
{
String namePub = servicio.Users.GetInfo().first_name + "ha actualizado su estado";
String cosa = servicio.Stream.Publish(richTextBox1.Text,
new attachment()
{
name = namePub,
href = "http://www.facebook.com/apps/application.php?id=136818146334647",
caption = "Tú tambien puedes usar Escuchando ahora, es facil y rapido",
properties = null,
media = new List<attachment_media>()
{
new attachment_media_image()
{
src="http://www.rammsdio.com.ar/images/img14781001.gif",
href = "http://www.facebook.com/apps/application.php?id=136818146334647"
}
}
},
new List<action_link>()
{
new action_link()
{
text="Visita SQLeros",
href="http://www.sqleros.com.ar/wps"
}
},null,0);
listBox1.Items.Add("Has actualizado tu estado...");
richTextBox1.Clear();
if (checkBox2.Checked)
servicio.Stream.AddLike(cosa);
}
catch (FacebookException fb)
{
listBox1.Items.Add(fb.Message);
}
Grettings i hope this its useful for you..ñ_ñ
Best Answer
For you question: you need to use the Facebook Connect API
For the sake of understanding: FB Connect is meant that way that you can easily log the user into his app and do everything on his behalf. offline_access is actually meant for cronjob work.