Make HTTP POST Request to Graph API using JavaScript SDK

  1. Previous
  2. Next
  3. Recent

    1. Signs 'He' May be Cheating on You
    2. 7 Vows of Hindu Marriage - Know What 'Saat Vachans' Mean
    3. RuPay vs Visa vs Mastercard vs American Express- A Comparison
    4. What is RuPay? Everything You Need to Know About India's Domestic Payment System!
    5. Top 10 Online Portals to Find a High Paying Job in India
    6. Top Indians who Revolutionized the IT Sector in India
    7. 8 Initiatives by Narendra Modi that Could Change the Future of the Nation
    8. 8 Work-from-home Jobs for Housewives, Students, and Part timers
    9. Complete Company Registration Process in India - Explained
    10. Change Name in India in 5 Steps - Filing Affidavit, Newspaper Ad & Gazette Notification

While going through Facebook’s documentation for developing apps, developers often find a line stating – ‘make HTTP POST Request to Graph API’. In fact, most newbies find it difficult to comprehend what is actually means. So today, we’ll see how we post on user wall  by making an HTTP POST Request to Facebook’s Graph API using JavaScript SDK.

To do this, first we need permission from the user to post on his wall. This is done by making an app and showing an authentication dialog box with ‘publish_stream’ permission. Here is the syntax for it,


<?php $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" . $app_id . " &redirect_uri=".urlencode($canvas_page)."&scope=publish_stream";

echo("<script> top.location.href='" . $auth_url . "'</script>"); ?>

So, for an app named(namespace) ‘post-on-user-wall’, with app_id=’12345689′, the authentication url would look like,


<?php $auth_url = "https://www.facebook.com/dialog/oauth?client_id=123456789 &redirect_uri=".urlencode('https://apps.facebook.com/post-on-user-wall')." &scope=publish_stream";

echo("<script> top.location.href='" . $auth_url . "'</script>"); ?>

Next step is to get the access token that’s required to perform actions on user’s Facebook account. To get the access token, we first load the Facebook’s php SDK file and then call its ‘getAccessToken()’ function. Here is the syntax,

<?php require_once('../facebook-php-sdk/src/facebook.php'); ?>
<?php $access_token = $facebook->getAccessToken(); ?>

Now its time to make HTTP POST Request to Graph API. We’ll do this using JavaScript SDK.

Here we’ll post the data entered by the user in a textbox. So lets first create a textBOX,


<form method='POST' id='flipform'>

 <textarea name='outputdata' id='outputdata' class='iodata'></textarea>
 <input type='button' class='but' value='Post my update' id='sub' onclick=postupdate('<?php echo $access_token; ?>')>

</form>

When the button is clicked, JavaScript function ‘postupdate()’ with access token as parameter is called. The function then makes call to JS.api function of JavaScript SDK.


<script src="http://connect.facebook.net/en_US/all.js"></script>

<script language="Javascript">

function postupdate(atoken)
{
 var msg=this.outputdata.value; FB.api('/me/feed', 'post', { access_token: atoken,message: msg }, function(response) {   if (!response || response.error) {     alert('Posting failed');   } else {     alert('Message successfully posted');   } });
}

</script>

We can also make request to Graph API by using Ajax call. JavaScript SDK, being 170kb+ in size can be sometimes problematic for the app.

blog comments powered by Disqus