0

我在使用 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.
                }
           }
4

1 回答 1

0

该代码完美运行...

<input type="button" id="fblogin" value="Login to Facebook" disabled="disabled" style="display:none"/>
        <fb:login-button v="2" length="long" onlogin="window.location = 'Default.aspx'">Login to Facebook</fb:login-button>

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: '<%: Facebook.FacebookApplication.Current.AppId %>',
            cookie: true,
            xfbml: true,
            oauth: true
        });

        function facebooklogin() {
            FB.login(function (response) {
                if (response.authResponse) {
                    // user authorized
                    // make sure to set the top.location instead of using window.location.reload()
                    top.location = '<%= this.ResolveCanvasPageUrl("~/") %>';
                } else {
                    // user cancelled
                }
            }, { scope: '<%: string.Join(",", ExtendedPermissions) %>' });
        };

        $(function () {
            // make the button is only enabled after the facebook js sdk has been loaded.
            $('#fblogin').attr('disabled', false).click(facebooklogin);
        });
    };
    (function () {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    } ());
</script>
于 2012-08-22T03:36:54.320 回答