开发者

adding events information to the facebook profile from 3rd party website

I am trying to add events information to the facebook users profile from a 3rd party website using Javascript SDK provided by facebook. I just googled for few tutorials and I got this links

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

I tried with the combination of login logout and session handling

this is the code which I am run just to test whether it works or not.

<!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 a开发者_如何学JAVAnd 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>

When I click on the add event page I am getting "undefined". Iam not able to add events to facebook profle. Kindly help me


I would try your code again, and maybe console.log(resp) in your callback method for the addevents function. I tested your code on fbrell.com and it seemed to work perfectly and as expected.

edit: after a little research (and your fixed code..) I found that the fb:login button doesn't ensure that your application has the correct permissions that the perms parameter requests. We find this out when we do a Graph request and it fails. So we catch the error, and then launch the Login modal via the FB.login method, and ask for the create_event permission. When the user accepts that dialog, we retry creating the 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>

Working code on fbrell.com: http://fbrell.com/saved/17c7b60eab91e6736a2a10c87d53c5b8


<!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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜