0

在此处输入代码我一直在关注 C# 的 Facebook 教程并获取访问令牌,我刚刚得到它,但我似乎无法理解他们教程中关于事件处理和重定向的步骤。我从来没有使用 JavaScript 到 C# 做过类似的事情。我有以下内容:

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: '{app Id}', // App ID
            status: true, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true  // parse XFBML
        });

        // Additional initialization code here

        FB.Event.subscribe('auth.authResponseChange', function (response) {
            if (response.status === 'connected') {
                // the user is logged in and has authenticated your
                // app, and response.authResponse supplies
                // the user's ID, a valid access token, a signed
                // request, and the time the access token 
                // and signed request each expire
                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;

                // TODO: Handle the access token

                // Do a post to the server to finish the logon
                // This is a form post since we don't want to use AJAX
                var form = document.createElement("form");
                form.setAttribute("method", 'post');
                form.setAttribute("action", '/FacebookLogin.ashx');

                var field = document.createElement("input");
                field.setAttribute("type", "hidden");
                field.setAttribute("name", 'accessToken');
                field.setAttribute("value", accessToken);
                form.appendChild(field);

                document.body.appendChild(form);
                form.submit();

            } else if (response.status === 'not_authorized') {
                // the user is logged in to Facebook, 
                // but has not authenticated your app
            } else {
                // the user isn't logged in to Facebook.
            }
        });
    };

    // Load the SDK Asynchronously
    (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 = "//connect.facebook.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
    }(document));
</script>

以及一个 Facebook 按钮,一切正常,但是根据教程在这里我需要“下一步,创建一个页面、操作或处理程序来接收令牌并重定向用户。对于这个例子,我们将创建一个通用处理程序。”

他们使用这个:

public class FacebookLogin : IHttpHandler, System.Web.SessionState.IRequiresSessionState {

    public void ProcessRequest(HttpContext context) {
        var accessToken = context.Request["accessToken"];
        context.Session["AccessToken"] = accessToken;

        context.Response.Redirect("/MyUrlHere");
    }

    public bool IsReusable {
        get { return false; }
    }
}

我只是把它放在我的代码后面,但它没有做任何事情。我只是不确定从这里做什么。如何让我的页面重定向到新页面,以便我可以执行以下操作:

var accessToken = Session["AccessToken"].ToString();
var client = new FacebookClient(accessToken);
dynamic result = client.Get("me", new { fields = "name,id" });
string name = result.name;
string id = result.id;
4

1 回答 1

0

确保您没有将 FacebokLogin 处理程序放在 aspx.cs 代码隐藏文件中。它需要是一个单独的 .ashx 文件。

在您的评论中,您说 FB.Event.subscribe('auth.authResponseChange', function (response) {...} 永远不会被调用。尝试在 FB.init 中包含 apiKey: "[OAuth token going here]"。不确定如果它会起作用,但值得一试。

例如

FB.init({
    appId: '{app Id}', // App ID
    status: true, // check login status
    cookie: true, // enable cookies to allow the server to access the session
    xfbml: true,  // parse XFBML
    apiKey: "92834yv221985vn4139y845vn19835vynv519835vn"
});
于 2014-01-16T10:31:48.477 回答