0

我正在使用 js sdk 并获取名称、位置、id 等基本信息。太好了,但我只需要一件事,那就是电子邮件。所以我在这里读到电子邮件需要扩展权限。我还在这里使用 fb.login 阅读了如何请求扩展权限。

在本教程的代码中,没有调用 fb.login ,但访问者会被提示登录并授予应用程序权限(如果不是)。这是怎么做的?注册按钮(根据教程稍作修改)是一个带有样式的类的 div。

<div class="fb-login-button" data-show-faces="false" data-width="400" data-max-rows="1">Register</div>

好吧,我检查了“注册”按钮,发现它呈现出相当大的变化,但我从未发现 onclick 或关于如何处理用户单击按钮事件的一些线索。我的猜测是来自样式的 iframe 有一个 src 并且事件必须在 fb 端。

所以回到我的脚本,我想也许 fb.login 和 fb.init 在一起,我可以在那里添加我的 perm 请求,但是没有 fb.login?我想也许可以把它放在下面的 else 分支中,但现在什么都没有,它可以工作......减去电子邮件的扩展权限?

window.fbAsyncInit = function ()
            {
                FB.init({...removed for concise....});

                //If user authorizes using fb account info:
                FB.Event.subscribe('auth.authResponseChange', function (response)
                {
                    if (response.status === 'connected') 
                    {
                        ...removed for concise code...
                    } 
                    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.
                        //**HERE IS WHERE I WOULD HAVE THOUGHT TO PUT FB.Login**
                    }
                });


            };

所以让我困惑的是这些碎片如何在拼图中组合在一起。我知道我需要请求扩展权限才能让 json 包含电子邮件,但不确定在哪里进行烫发。请求在???

这里的代码是页面中的实际代码,因此您可以看到我的整个 sdk 实现。

<div id="fb-root"></div>
        <script>
            window.fbAsyncInit = function ()
            {
                FB.init({
                    appId: 12324, // App ID
                    status: true, // check login status
                    cookie: true, // enable cookies
                    xfbml: true  // parse XFBML
                });

                //If user authorizes using fb account info:
                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;

                        // send access token to server so we can fill in fields
                        var form = document.createElement("form");
                        form.setAttribute("method", 'post');
                        form.setAttribute("action", '/mypage');

                        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>
4

1 回答 1

1

由于您不是使用 SDK 本身登录,而是使用登录按钮,因此您必须通过scope参数请求权限,设置为data-scope="…"按钮本身,请参阅https://developers.facebook.com/docs/reference/plugins/登录/

另一种选择是删除按钮,并FB.login在您自己制作的按钮/链接上调用 onclick。

于 2013-02-21T23:19:26.297 回答