如何在墙上设置发布消息的权限对话框

时间:2011-12-08 14:27:08

标签: javascript facebook cordova

我在Facebook上创建了应用程序。通过使用Facebook Javascript SDk,我从android phonegap应用程序发布消息到Facebook墙。但我想在墙上的消息发布之前显示权限对话框 (即)当我点击权限对话框中的“允许”按钮消息应该贴在墙上,而我点击“不允许”按钮时,消息不应发布在墙上。 我的代码是:

<body>
<div id='fb-root'></div>
<script src='all.js'></script>
<input type="text" value="User ID" name="user_ids" />
           <p><a onclick='postToFeed(); return false;'>Post to Feed</a></p>
            <p id='msg'></p>
    <script> 
      FB.init({appId: "xxxxxxxx", status: true, cookie: true});

      function postToFeed() 
    {
                    var user_ids = document.getElementsByName("user_ids")[0].value;
                   alert(user_ids);

                    FB.api('/me/feed', 'post', { message: user_ids}, function(response) {
                           if (!response || response.error) {
                            alert('Error occured');
                           } else {
                           alert('Post ID: ' + response.id);
                           }
                   });
      }
   </script>
  </body>

上面的代码托管在服务器上。在phonegap(index.html)中,我使用window.open方法调用了url,它将显示文本框和按钮。当我点击按钮时,(从文本框中获取值)消息应该贴在墙上  如何为此编码设置权限对话框或如何在FB.api中传递{perms:'publish_stream'}('/ me / feed')

请给我一些建议/想法

提前致谢。

1 个答案:

答案 0 :(得分:0)

我不知道我的问题是否正确,但我建议: 首先请求权限,然后在下一步显示发送按钮或自动发送帖子 - 您想要什么。不是两个在同一时间(我只是没有得到这个概念)。以下是一些可能对您有所帮助的代码:

window.fbAsyncInit = function() {
    FB._https = true;
    FB.init({
        appId : 'APP_ID',
        status : true, 
        cookie : true, 
        oauth : true 
    });

    // Check whether the user is connected to the app
    FB.getLoginStatus(function(response) {
        if (response.status !== 'connected') {
            // The user has not authorized the app
            authorizeApp();
        } else {
            // The user has already authorized the app
            var fbuid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            FB.api('/me', function(response) {
                // Check for publish_stream extended permission
                var query = FB.Data.query('select publish_stream from permissions where uid={0}', fbuid);
                query.wait(function(rows) {
                    if(rows[0].publish_stream == 1) {
                        // The user has granted the extended permission
                        postToWall();
                    } else {
                        // The user has not granted the extended permission
                        authorizeApp();
                    }
                });
            });
        }
    });
};
(function() {
    var e = document.createElement('script');
    e.type = 'text/javascript';
    e.src = document.location.protocol + '//connect.facebook.net/de_DE/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());

function authorizeApp() {
    // Redirect the user back after authorization
    var path = 'https://www.facebook.com/dialog/oauth?';
    var queryParams = ['client_id=APP_ID', 'redirect_uri=REDIRECT_URI', 'response_type=token', 'scope=publish_stream'];
    var query = queryParams.join('&');
    var url = path + query;
    top.location.href = url;
}

// OR
function authorizeAppInPopup() {
    FB.login(function(response) {
        if (response.authResponse) {
            // User authorized app
            postToWall();
        } else {
            // User cancelled login or did not fully authorize
        }
    }, {scope: 'publish_stream'});
}