我在使用 FB API 方面是全新的,我正在尝试从我的 Asp.net 应用程序发布到 facebook 墙上。
我从 FB 获得了 Appkey 和密钥,只是试图按照代码在 FB 墙上发布。
链接: http: //kennetham.com/2010/07/21/facebook-api-asp-net/
我现在面临的问题是,在我的 ConnectAuthentication 类中,HttpContext.Current.Request.Cookies[fullCookie] 始终为 NULL。因此,当我在我的页面加载中通过“if (ConnectAuthentication.isConnected())”检查 FB 连接时,它总是返回 false 并且它不会在条件内运行代码。
这是为什么?我错过了什么吗?
ConnectAuthentication 类
public class ConnectAuthentication
{
public ConnectAuthentication()
{
}
public static bool isConnected()
{
return (SessionKey != null && UserID != -1);
}
public static string ApiKey
{
get
{
return ConfigurationManager.AppSettings["APIKey"];
}
}
public static string SecretKey
{
get
{
return ConfigurationManager.AppSettings["Secret"];
}
}
public static string SessionKey
{
get
{
return GetFacebookCookie("session_key");
}
}
public static long UserID
{
get
{
long userID = -1;
long.TryParse(GetFacebookCookie("user"), out userID);
return userID;
}
}
private static string GetFacebookCookie(string cookieName)
{
string retString = null;
string fullCookie = ApiKey + "_" + cookieName;
if (HttpContext.Current.Request.Cookies[fullCookie] != null)
retString = HttpContext.Current.Request.Cookies[fullCookie].Value;
return retString;
}
}
以下是在我的页面加载中使用 ConnectAuthentication 类的方式:
if (ConnectAuthentication.isConnected())
{
Facebook.Session.ConnectSession session = new Facebook.Session.ConnectSession(ConnectAuthentication.ApiKey, ConnectAuthentication.SecretKey);
_connectSession = new ConnectSession(ConnectAuthentication.ApiKey, ConnectAuthentication.SecretKey);
Api _facebookAPI = new Api(_connectSession);
_connectSession.UserId = ConnectAuthentication.UserID;
Facebook.Rest.Api api = new Facebook.Rest.Api(_connectSession);
//Display user data captured from the Facebook API.
Facebook.Schema.user user = api.Users.GetInfo();
string fullName = user.first_name + " " + user.last_name;
Panel1.Visible = true;
Label1.Text = fullName;
}
else
{
//Facebook Connect not authenticated, proceed as usual.
}
}