0

我正在尝试使用 facebook 提供的 Javascript SDK 从第三方网站将事件信息添加到 facebook 用户配置文件中。我刚刚搜索了一些教程,我得到了这个链接

http://thinkdiff.net/facebook/graph-api-javascript-base-facebook-connect-tutorial/

http://www.masteringapi.com/tutorials/how-to-ask-for-extended-permission-in-your-facebook-application/32/

http://www.masteringapi.com/tutorials/how-to-create-facebook-events-using-graph-api/49/#using_the_js-sdk

我尝试结合登录注销和会话处理

这是我运行的代码,只是为了测试它是否有效。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>

<div id="fb-root"></div>

<script type="text/javascript">
window.fbAsyncInit = function()
{
    FB.init({
        appId: '207712505927107', 
        status: true, 
        cookie: true, 
        xfbml: true
    });

    /* All the events registered */
    FB.Event.subscribe('auth.login', function(response) {
    // do something with response
        login();
    });

    FB.Event.subscribe('auth.logout', function(response) {
        // do something with response
        logout();
    });

    FB.getLoginStatus(function(response)
    {
        if (response.session)
        {
            // logged in and connected user, someone you know
            login();
        }
    });
};

(function() 
{
    var e = document.createElement('script');
    e.type = 'text/javascript';
    e.src = document.location.protocol
            + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());

function login()
{
    FB.api('/me', function(response)
    {
        document.getElementById('login').style.display = "block";

        var loggedInMessage = response.name + " successfully logged in!";
        document.getElementById('login').innerHTML = loggedInMessage;
    });
}

function logout()
{
    document.getElementById('login').style.display = "none";
}

//addevent to facebook profile 

function addevents()
{
    FB.api('/me/events', 'post', {
        name:          "JS-SDK Event",
        start_time:    1272718027,
        location:      "Beirut",
        privacy_type:  "OPEN"
    }, function(resp) {
        alert(resp.id);
    });
}
</script>

<p>
    <fb:login-button
        autologoutlink="true" 
        perms="email,user_birthday,status_update,publish_stream,create_event">
    </fb:login-button>
</p>

<p>
    <a href="#" onclick="addevents(); return false;">Add Events</a>
</p>

<br />
<br />
<br />
<br />

<div id="login" style ="display:none"></div>
<div id="name"></div>

</body>
</html>

当我单击添加事件页面时,我得到“未定义”。我无法将事件添加到 facebook 配置文件。请帮助我

4

2 回答 2

1

我会再次尝试您的代码,并且可能在您的 addevents 函数的回调方法中使用 console.log(resp) 。我在 fbrell.com 上测试了您的代码,它似乎可以正常工作并且符合预期。

编辑:经过一番研究(以及您的固定代码..),我发现 fb:login 按钮不能确保您的应用程序具有 perms 参数请求的正确权限。当我们执行 Graph 请求并且它失败时,我们会发现这一点。所以我们捕捉到错误,然后通过该FB.login方法启动登录模式,并请求create_event权限。当用户接受该对话框时,我们重试创建事件。

<fb:login-button
    autologoutlink="true" 
    perms="email,user_birthday,status_update,publish_stream,create_event">
</fb:login-button>
<a href="#" id="create_event" onclick="create_event();">create</a>

<script>
FB.XFBML.parse();
document.getElementById('create_event').onclick = function() { create_event(); }
function create_event()
{
    FB.api('/me/events', 'post', {
        name:          "JS-SDK Event",
        start_time:    1272718027,
        location:      "Anywhere USA",
        privacy_type:  "OPEN"
    }, function(resp) {

        if (typeof resp.error != 'undefined')
        { /*this failed. most likely because we don't have the extended permission
            for create_event. so lets check for it, ask for it, and try to create
            the event again.  but first, we'll make sure that the error message says
            something about 'create_event' so we don't launch an infinite loop
          */
            if (resp.error.message.match(/create_event/))
            {
                FB.login(function(response)
                {
                    if (response.session 
                        && response.status == 'connected' 
                        && response.perms.match(/create_event/))
                    {
                        //this user is connected & we have create event permission.
                        create_event();
                    }
                },
                {
                    perms: 'create_event'
                });
            }
        }    
    });
}
</script>

fbrell.com 上的工作代码:http: //fbrell.com/saved/17c7b60eab91e6736a2a10c87d53c5b8

于 2011-05-04T02:37:32.210 回答
0
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div id="fb-root"></div>
<script type="text/javascript">

window.fbAsyncInit = function() {
    FB.init({appId: '207712505927107', status: true, cookie: true, xfbml: true});

    /* All the events registered */
    FB.Event.subscribe('auth.login', function(response) {
        // do something with response
        login();
    });

    FB.Event.subscribe('auth.logout', function(response) {
        // do something with response
        logout();
    });

    FB.getLoginStatus(function(response) {
        if (response.session) {
            // logged in and connected user, someone you know
            login();
        }
    });
};

(function() {
    var e = document.createElement('script');
    e.type = 'text/javascript';
    e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());

function login(){
    FB.api('/me', function(response) {
        document.getElementById('login').style.display = "block";
        document.getElementById('login').innerHTML = response.name + " succsessfully logged in!";
    });
}

function logout(){
    document.getElementById('login').style.display = "none";
}

//add events 
function addevents() {
    FB.api('/me/events','post',{
        name:"awesome event",
        start_time:1272718027,
        location:"Bengaluru",
        privacy_type:"OPEN",
        description:"U r    invited !!!"
    }, function(resp) {
        document.write(resp.id);
    });
}

//checking persmission

function check_perm() {

    FB.api({ method: 'users.hasAppPermission', ext_perm: 'create_event' }, function(resp)
    {
        if (resp === "1") {
            alert('Permission granted');
        } else {
            alert("Permission not granted");
        }
    });
}

</script>

<p>Please click on Login button first </p>
<p><fb:login-button autologoutlink="true" perms="create_event"></fb:login-button></p>

<p> Please click on check permissions first </p>
<a href="#" onclick="check_perm(); return false;"> check permissions </a>
<p> now u r all set to add events to facebook </p>

<p>
    <a href="#" onclick="addevents(); return false;"> Add Events </a>

</p>

<br /><br /><br />

<div id="login" style ="display:none"></div>
<div id="name"></div>

</body>
</html>
于 2011-05-04T12:09:05.813 回答