DownLoad Facebook jar
To work in JAVA Download - facebook-java-api-3.0.2-bin.zip
Inlcuded the jar in the class path and try to access the class in the jar
IDE
Eclipse - Version: 3.4.0
Get the API key & Application Secret
To start your work with Facebook API, You need to get the
API key & Application Key.
You need to get register your applications in this page http://www.facebook.com/developers/apps.php using Facebook account.
Necessary fields to be entered as follows
In Basic - Include the developers name .Who will be involving in developing the application.
In Canvas –Give the callback url of your application
We can start coding the application now.
Create the Twitter object & get the request token by passing the application Consumer key & secret.
To get the user client
FacebookJsonRestClient class is used for getting user client returns the object in JSON format.
FacebookJsonRestClient userClient =getUserClientsession);
public static FacebookJsonRestClient.getUserClient(HttpSession session) {
return (FacebookJsonRestClient)session.getAttribute(FACEBOOK_USER_CLIENT);
}
User session doesn't have a Facebook API client setup yet.
Pass the API key & Application secret to get the valid userclient
And store it in a session.
userClient = new FacebookJsonRestClient(api_key, secret);
session.setAttribute(FACEBOOK_USER_CLIENT, userClient);
Pass the Facebook url with your api key & required permission redirect the call.
You can check the permission available in Facebook .
facebook = new FacebookWebappHelper(req, res, api_key, secret);
nextPage = req.getRequestURI();
nextPage = nextPage.substring(nextPage.indexOf("/", 1) + 1);
//cut out the first /, the context path and the 2nd /
System.out.println(nextPage);
boolean redirectOccurred = facebook.requireLogin(nextPage);
if(redirectOccurred){
res.sendRedirect("http://www.facebook.com/login.php?api_key=dbfc65d0e72f5c103gg0gfgd8c5&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,sms,email,user_location");
return;
}
After execution of the above code.It request you enter the Facebook username & password.
Get the auth_token
String authToken=request.getParameter("auth_token");
By passing this authToken to the client you can start working on the functionalities like getting friends ,knowing user details etc.,
You can check the functionalities available in the FacebookJsonRestClient in this link
http://developers.facebook.com/docs/reference/rest/
Functions
To know the current user id
Long facebookUserID = userClient.users_getLoggedInUser();
To get friends Id’s
JSONArray arrayObj = (JSONArray)userClient.friends_get();
Get the id’s from Array & store it in a list
List userIds = new ArrayList();
for(int i=0;itry {
userIds.add((arrayObj.getString(i));
} catch (JSONException e) {
e.printStackTrace();
}
}
Set the necessary fields in the ProfileField,that you need from a
User.
EnumSetfields = EnumSet.of ( com.google.code.facebookapi.ProfileField.NAME, com.google.code.facebookapi.ProfileField.PIC, com.google.code.facebookapi.ProfileField.CURRENT_LOCATION, com.google.code.facebookapi.ProfileField.FIRST_NAME, com.google.code.facebookapi.ProfileField.LAST_NAME);
To get the user details
JSONArray userArray = null;
try {
userArray = client.users_getInfo(userIds, fields);
} catch (FacebookException e1) {
e1.printStackTrace();
}
for(int i=0;i< userArray.length();i++){
try {
for (int j = 0; j < userArray.length(); j++) {
JSONObject obj = userArray.getJSONObject(j);
System.out.println("User Id"+obj.getString("uid"));
System.out.println("Pic url "+obj.getString("pic"));
System.out.println("Name"+obj.getString("first_name")
+" "+obj.getString "last_name"));
}
} catch (JSONException e) {
e.printStackTrace();}
}
To update the status of the current user
Check whether the user having the permissions & than try the functions
if (userClient.users_hasAppPermission(Permission.STATUS_UPDATE)){
userClient.users_setStatus("Status-Developing Facebook apps in Java!", false);
}
To Publish the stream for the current user
if(userClient.users_hasAppPermission(Permission.PUBLISH_STREAM)){
userClient.stream_publish("Stream-Publish-Wall written using Facebook API !", null, null, null, null);
}
To post the link to single /Group of user
userClient.links_post(Long.parseLong("Give the UserID"), "www.kyyba.com", "IT Recruitment");
No comments:
Post a Comment