How to send an post using Facebook API

The biggest thing people want to do when  they do a call to Facebook API is do a post to theirs or someone else’s Facebook profile. To do this you need to be logged in, have permissions and then do the API call to post. Here Pure Random will describe how to call to Facebook API for not just a standard Facebook post, but also how to send a picture and how to send a link to Facebook.

As with all Facebook API calls you need to have the call to set up the connection to the Facebook API which is below. Simply put this above where you are doing the Facebook call, but if you already have it in then you don’t need to add it again. The best method would be to create Facebook namespace with all the calls in one file. This would then prevent you calling functions twice and tidy up the JavaScript.

window.fbAsyncInit = function () {
    FB.init({
       appId: LoginCall.getCookie(‘fb_id’),
        cookie: true,  // enable cookies to allow the server to access
       // the session
       xfbml: true,  // parse social plugins on this page
       version: ‘v2.2’ // use version 2.1
       });

// Load the SDK asynchronously
   (function(d, s, id) {
   var js, fjs = d.getElementsByTagName(s)[0];
   if (d.getElementById(id)) return;
   js = d.createElement(s); js.id = id;
   js.src = “//connect.facebook.net/en_US/sdk.js”;
   fjs.parentNode.insertBefore(js, fjs);
   }(document, ‘script’, ‘facebook-jssdk’));

//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js

(adsbygoogle = window.adsbygoogle || []).push({});

How to send a post

To make things easier and neater as said before you should put it in a namespace. Therefore that is how I will demonstrate it below. I have put the Facebook calls in a namespace ‘FBcall’.

Var Fbcall = {};

For the user to post to Facebook they need to be logged in, so before we do the call we check the user has given you permissions to their Facebook profile by doing the ‘FB.login’ API call. There is more about the login to Facebook API call http://www.purerandom.co.uk/javascript/how-to-use-facebook-api-to-login

You can also see we have put in the scope for ‘publish_actions’, this is the scope call to get permissions from the user to publish content to their profile, which you must have.

//Post Facebook Message
FBcall.PostTOfb = function(PostMessage,PostImage,PostLink){
    FB.login(function (response) {
       FBcall.statusChangeCallback(response);
   }, { scope: ‘publish_actions,public_profile,email’ });
}

To make the call easier we pass over the ‘PostMessage’, PostImage’ and ‘PostLink’ to the function. This way you can send a whole post with just one action. After we have checked the user is logged in and passed over all the parameters, we can check if there is content in the variables. We don’t want to send empty information over as it could make the post look weird and Facebook could refuse the information. Therefore we do a quick If/Else check of length on the parameters and if they do have content then we fill the JavaScript Object with the variables. This is one of the parameters that Facebook requires, which is a JavaScript Object with all the Facebook post information.

//Post Facebook Message
FBcall.PostTOfb = function(PostMessage,PostImage,PostLink){
    FB.login(function (response) {
        FBcall.statusChangeCallback(response);
        console.log(response.authResponse.accessToken);

        var PostParams = {};

        if (PostMessage.length > 0){ PostParams[“message”] = PostMessage; }
        if (PostImage.length > 0){ PostParams[“source”] = PostImage; }
        if (PostLink.length > 0){ PostParams[“link”] = PostLink; }

    }, { scope: ‘publish_actions,public_profile,email’ });
}

You may have seen the next call before in the login phase of getting the users profile details. This is the ‘FB.api’ call that you pass the action you want to execute in the next slot, which we are doing ‘/me/feed’. This is what will tell Facebook that you want to send information to the feed. In the next slot we send the key word ‘post’ to tell Facebook to post the information. The following slot is for the JavaScript Object we created earlier, that has all the content to post to Facebook. The final slot is for the Call-back from Facebook after we send the request. This will response with the error or with the Facebook post’s ID if it is successful.

//Post Facebook Message
FBcall.PostTOfb = function(PostMessage,PostImage,PostLink){
    FB.login(function (response) {
        FBcall.statusChangeCallback(response);
        console.log(response.authResponse.accessToken);

        var PostParams = {};

        if (PostMessage.length > 0){ PostParams[“message”] = PostMessage; }
        if (PostImage.length > 0){ PostParams[“source”] = PostImage; }
        if (PostLink.length > 0){ PostParams[“link”] = PostLink; }

       FB.api(‘/me/feed’, ‘post’, PostParams,
          function(response) {
             if (!response || response.error) {
                console.log(JSON.stringify(response.error));
             }
             else {
                console.log(‘Action was successful! Action ID: ‘ + response.id);
             }
          }
       )

    }, { scope: ‘publish_actions,public_profile,email’ });
}

Here is then an example of a call to post content to Facebook:

  • We are passing all the parameters over in this call in string format.
  • The first is just the string of the post you want to show on Facebook.
  • The next is the URL of the Image you want to send over.
  • Finally the link to the page you want the link to go to. This must include the ‘http://’ or the ‘http://’.

FBcall.PostTOfb(‘How to post to Facebook by Pure Random’,’http://www.purerandom.co.uk/image/pureran/logo.png’,’http://www.purerandom.co.uk’);

If you need any help with any errors you find, please feel free to contact us at info@purerandom.co.uk

Advertisements

Leave a message please

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s