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({
    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 に答える