Posting messages on facebook wall using graph api

14 / Dec / 2010 by Anshul Sharma 2 comments

Firstly ,you need to connect with facebook.

Once you have facebook access token you can post messages on the facebook wall using the below code.

void postMessage(String facebookAccessToken, String message, String facebookId)  {
            StringBuilder stringBuilder = new StringBuilder("access_token=");
            stringBuilder.append(URLEncoder.encode(facebookAccessToken, "UTF-8"));
            stringBuilder.append("&message=");
            stringBuilder.append(URLEncoder.encode(message, "UTF-8"));
            URL url = new URL("https://graph.facebook.com/${facebookId}/feed");
            HttpURLConnection connection
            try {
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestProperty("Content-Length", "" + stringBuilder.toString().length());
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
                outputStreamWriter.write(stringBuilder.toString());
                outputStreamWriter.flush();
            } finally {
                connection?.disconnect()
            }
    }
 

Hope this helped!

Cheers!

Anshul Sharma

FOUND THIS USEFUL? SHARE IT

comments (2)

  1. Henry Dorrell

    I definitely wanted to write a small note in order to appreciate you for all of the great techniques you are giving out at this site. My particularly long internet investigation has at the end of the day been paid with useful knowledge to exchange with my contacts. I ‘d suppose that many of us website visitors are quite blessed to be in a wonderful network with many marvellous professionals with helpful tips and hints. I feel quite grateful to have discovered the web site and look forward to really more awesome times reading here. Thank you again for a lot of things.

    Reply
  2. Ati

    Hi Anshul Sharma,
    Can u pls tell which api u r using ,what else u r importing AS i am getting the following error java.net.UnknownHostException: graph.facebook.com

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *