Search Google

Friday, 23 January 2015

Post to your Facebook wall using Javascript SDK

Post to your Facebook wall using Javascript SDK

Step1:
1
2
3
4
5
<div id="fb_div">
    <h3>Post to your Facebook wall:</h3> <br />
    <textarea id="fb_message" name="fb_message" cols="70" rows="7"></textarea> <br />
    <input type="button" value="Post on Wall" onClick="post_on_wall();" />
</div>
Step 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
window.fbAsyncInit = function()
{
    FB.init({
        appId  : 'xxxxxxxxxxxxxxx',
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true , // parse XFBML
        oauth : true // Enable oauth authentication
    });
 
 
};
 
function post_on_wall()
{
    FB.login(function(response)
    {
        if (response.authResponse)
        {
            alert('Logged in!');
 
            // Post message to your wall
 
            var opts = {
                message : document.getElementById('fb_message').value,
                name : 'Post Title',
                link : 'www.postlink.com',
                description : 'post description',
            };
 
            FB.api('/me/feed', 'post', opts, function(response)
            {
                if (!response || response.error)
                {
                    alert('Posting error occured');
                }
                else
                {
                    alert('Success - Post ID: ' + response.id);
                }
            });
        }
        else
        {
            alert('Not logged in');
        }
    }, { scope : 'publish_stream' });
}
Step3:
create a div with id=”fb-root” and paste below code after this div
1
<div id="fb-root"></div>
Step4:
1
2
3
4
5
6
7
8
(function() {
    var e = document.createElement('script');
    // replacing with an older version until FB fixes the cancel-login bug
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    //e.src = 'scripts/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());

No comments:

Post a Comment