【问题标题】:Facebook - JavaScript SDK - How to ask for permission automaticallyFacebook - JavaScript SDK - 如何自动请求许可
【发布时间】:2012-10-28 05:24:25
【问题描述】:

我正在使用 HTML 5 开发一些东西并在其中插入 Facebook JavaScript SDK。

我已经开发了一个 Facebook 应用程序,我想:

1) 如果用户没有登录facebook,它会要求登录,但是如果用户登录并打开应用程序,它应该自动请求权限,而不是用户需要点击@987654322的链接再次@。

有一个 Stack Overflow 问题,How do I get my Facebook application to automatically ask for the required permissions post installation 他们使用window.top.location = "<?php echo $loginUrl; ?>"; 来解决这个问题,但是我如何在 JavaScript 中找到这个身份验证 URL?

JavaScript 代码:

<div id="fb-root"></div>
<script>
    // 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));

    // Init the SDK upon load
    window.fbAsyncInit = function() {
        FB.init({
            appId      : 'appid', // App ID
            channelUrl : '//www.domain.com/channel.html', // Channel file
            status     : true, // Check login status
            cookie     : true, // Enable cookies to allow the server to access the session
            xfbml      : true  // Parse XFBML
        });

        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;

                document.getElementById('auth-loggedout').style.display = 'none';
                document.getElementById('auth-loggedinnotauthenticate').style.display = 'none';
                document.getElementById('loginauthenicated').style.display = 'block';
            }
            else
                if (response.status === 'not_authorized') {
                    window.top.location = "https://www.facebook.com/dialog/oauth?client_id=id&redirect_uri=http://apps.facebook.com/appp/";
                }
                else {
                    window.top.location = "https://www.facebook.com/login.php?api_key=id&skip_api_login=1&display=page&cancel_url=http://apps.facebook.com/appp/";
                }
    });
</script>

【问题讨论】:

    标签: javascript facebook


    【解决方案1】:

    使用这个:

    FB.getLoginStatus(function(response) {
        if (response.authResponse) {
            fbUserId = response.authResponse.userID;
            token = response.authResponse.accessToken;
        }
        else {
            FB.login(function(response) {
                if (response.authResponse) {
                    fbUserId = response.authResponse.userID;
                    token = response.authResponse.accessToken;
                }
                else {
                     console.log('User cancelled login or did not fully authorize.');
                }
            }, {scope: 'user_location'});
        }
    },true);
    

    【讨论】:

      【解决方案2】:

      我认为对您来说最好的选择是使用authenticated referrals 功能,这意味着 Facebook 会为您处理身份验证以及您需要的权限。

      只有在用户通过身份验证过程后,用户才会被传递给您的应用程序。

      如果出于任何原因这不适合您,请查看authentication 的文档,尤其是server-side flow,因为看起来这就是您所写的内容。

      从您的 JavaScript 代码看来,您在初始化 Facebook SDK 时似乎没有使用频道 URL 属性。正如JavaScript SDK documentation频道文件部分)所述:

      频道文件解决了跨域通信的一些问题 在某些浏览器中

      然后:

      channelUrl 参数是可选的,但建议使用。

      我不能保证它会修复它,但它可能会。

      应该是这样的:

      // Init the SDK upon load
      window.fbAsyncInit = function() {
          FB.init({
              appId      : 'appid', // App ID
              channelUrl : '//www.domain.com/channel.html', // Channel File
              status     : true, // Check login status
              cookie     : true, // Enable cookies to allow the server to access the  session
              xfbml      : true  // Parse XFBML
          });
      
          FB.getLoginStatus(function(response) {
              if (response.status === 'connected') {
                  var uid = response.authResponse.userID;
                  var accessToken = response.authResponse.accessToken;
      
                  document.getElementById('auth-loggedout').style.display = 'none';
                  document.getElementById('auth-loggedinnotauthenticate').style.display = 'none';
                  document.getElementById('loginauthenicated').style.display = 'block';
              }
              else
                  if (response.status === 'not_authorized') {
                      window.top.location = "https://www.facebook.com/dialog/oauth?client_id=id&redirect_uri=http://apps.facebook.com/appp/";
                  }
                  else {
                      window.top.location = "https://www.facebook.com/login.php?api_key=id&skip_api_login=1&display=page&cancel_url=http://apps.facebook.com/appp/";
                  }
      });
      
      // 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));
      

      如您所见,首先需要定义window.fbAsyncInit回调方法,然后才加载SDK。

      【讨论】:

      • 您好 Nitzan,感谢您提供详细信息。 facebook.com/dialog/oauth?client_id=myid&redirect_uri=http:/… 这是可行的,但我尝试使用 3 个帐户。在某些帐户中,当用户被重定向到此字符串时,它显示“发生错误。请稍后再试。”你能给出任何想法吗?
      • redirect_uri 不应该像你写的那样是 facebook 域 url,你应该重定向到你自己的服务器,然后你需要处理你想要的,然后将用户重定向到 @ 987654326@....
      • 但他们提到我们可以在developers.facebook.com/docs/authentication/server-side 中这样做。这在一段时间之前也有效。是不是和js加载不正确的网络慢有关
      • 是的,你是对的,它确实说在那里,我的错。该帐户究竟在什么时候收到该错误?
      • 有时.. 现在它工作正常。但似乎问题似乎与此 stackoverflow.com/questions/9884878/… 有关,如果用户未登录并且他们打开应用程序页面..我提供与 fb.login() 的链接,该链接要求用户许可。但一旦用户给予许可。它确实会重新加载页面,所以我的主屏幕在我刷新它之前不会出现
      猜你喜欢
      • 2011-10-09
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多