Facebook Javascript API - 发布状态更新

时间:2014-03-28 16:36:30

标签: javascript facebook facebook-graph-api

我试图找到如何使用Javascript API向Facebook发布用户状态更新的完整说明。

我已经查看了stackoverflow上的其他问题和答案,但没有一个提供有用的答案(大多数只是告诉你去看看Facebook的文档......缺少javascript)< / p>

我已成功记录用户的代码,但我似乎无法正确请求权限。

Facebook响应错误如下所示: (#200)用户尚未授权应用程序执行此操作

我一直在尝试在他们登录后请求权限,因此如果他们访问网页并且已经登录,则会向他们请求权限。不幸的是,添加以下行并不起作用:

FB.Connect.showPermissionDialog(&#39; publish_stream&#39);

任何帮助都将不胜感激,但请不要只是转发我关于权限或Javascript的Facebook文档,我已经完成了两者的文档,并且无法弄明白。

到目前为止,我有:

//Initialize facebook
     window.fbAsyncInit = function() {
      FB.init({
        appId      : '243153685858721',
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
      });

      // Here we subscribe to the auth.authResponseChange JavaScript event. This event is fired
      // for any authentication related change, such as login, logout or session refresh. This means that
      // whenever someone who was previously logged out tries to log in again, the correct case below 
      // will be handled. 
      FB.Event.subscribe('auth.authResponseChange', function(response) 
      {
        // Here we specify what we do with the response anytime this event occurs. 
        if (response.status === 'connected') {
          // The response object is returned with a status field that lets the app know the current
          // login status of the person. In this case, we're handling the situation where they 
          // have logged in to the app.
            FB.Connect.showPermissionDialog('publish_stream');
            FB.api('/me/feed', 'post', { message: body }, function(response2) 
            {
                console.log(response);

                if (!response || response.error) {
                console.log("error");
                console.log(response2);
                console.log(response.error);
                alert("You haven't given our app permissions to post to your wall, please logout of Facebook, then back");
                } else {
                alert('Post ID: ' + response.id);
              }
            });
        } else if (response.status === 'not_authorized') {
          // In this case, the person is logged into Facebook, but not into the app, so we call
          // FB.login() to prompt them to do so. 
          // In real-life usage, you wouldn't want to immediately prompt someone to login 
          // like this, for two reasons:
          // (1) JavaScript created popup windows are blocked by most browsers unless they 
          // result from direct interaction from people using the app (such as a mouse click)
          // (2) it is a bad experience to be continually prompted to login upon page load.
          FB.login();
        } else {
          // In this case, the person is not logged into Facebook, so we call the login() 
          // function to prompt them to do so. Note that at this stage there is no indication
          // of whether they are logged into the app. If they aren't then they'll see the Login
          // dialog right after they log in to Facebook. 
          // The same caveats as above apply to the FB.login() call here.
          FB.login();
        }
        });
      };

0 个答案:

没有答案