0

当我使用我的应用程序中的以下链接以防用户未登录 Facebook 时,Facebook 登录显示,然后用户验证应用程序,然后应用程序显示。

<a href="https://www.facebook.com/dialog/oauth?client_id=521577774587246&redirect_uri=http://localhost:55549/fb/&scope=email,read_stream">Facebook</a>

如果用户已登录并从 Facebook 搜索框中搜索应用程序,则会显示带有 Facebook 登录按钮的 default.aspx。

如何向用户显示授权对话框而不是登录按钮?

以下是 default.aspx javascript -

<script type="text/javascript">
        window.fbAsyncInit = function () {
            FB.init({
                appId: '521577774587246', // 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", 'loadMainPage.ashx');

                    var field = document.createElement("input");
                    field.setAttribute("type", "hidden");
                    field.setAttribute("name", 'accessToken');
                    field.setAttribute("value", accessToken);
                    form.appendChild(field);
                    //                    alert(accessToken);
                    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

                    window.top.location = "https://www.facebook.com/dialog/oauth?client_id=521599834587246&redirect_uri=http://localhost:55549/fb/&scope=email,read_stream";
                } 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>
4

1 回答 1

0

正如这篇文章所建议的, 我使用 FB.login 代替FB.Event.subscribe('auth.authResponseChange',现在它要求授权。

于 2013-11-13T11:37:11.380 回答