Thursday, March 25, 2010

Facebook java Example to send message (or) Feed to wall

First download the Facebook Api :
Clickhere

and copy the lib to your project lib

Click on the below link for basic setup with Facebook:
Click here

After that use this below example to Test the message:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;

import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookJsonRestClient;

/**
* Servlet implementation class Test
*/
public class Test extends HttpServlet {
String FB_APP_API_KEY = new String("your APIKEY");
String FB_APP_SECRET = new String("Your secret");
String FB_SESSION_KEY = new String("your session");

private static final long serialVersionUID = 1L;


/**
* @see HttpServlet#HttpServlet()
*/
public Test() {

super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
FacebookJsonRestClient facebook = new FacebookJsonRestClient(
FB_APP_API_KEY, FB_APP_SECRET, FB_SESSION_KEY);

// FacebookJsonRestClient facebookClient2 =
// (FacebookJsonRestClient)facebook.getFacebookRestClient();
FacebookJsonRestClient facebookClient = (FacebookJsonRestClient) facebook;
try {
facebookClient.stream_publish("Sample Message From:java App2", null, null, null, null);
} catch (FacebookException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
JSONArray response1 = (JSONArray)facebook.friends_get();
System.out.println("Friends->"+response1);
} catch (FacebookException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("successfully updated");

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

use the Following url to get session using your API KEY:

http://www.facebook.com/login.php?api_key=YOURAPIKEY&connect_display=popup&v=1.0&next=http://www.facebook.com/connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&session_key_only=true&req_perms=read_stream,publish_stream,offline_access








1 comment:

  1. hello!!please help me I tried to get session key with the url http://www.facebook.com/login.php?api_key=YOURAPIKEY&connect_display=popup&v=1.0&next=http://www.facebook.com/connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&session_key_only=true&req_perms=read_stream,publish_stream,offline_access,i can't get the session key!it saw the probleme

    ReplyDelete