Steps to integrate facebook with Java

Facebook Java Integration

Facebook Java Integration

ພາບ​ລວມ: Social media like facebook, linked-in, twitter are become popular for web marketing. The exposure to social media has a tremendous impact on different fields of marketing, promotion etc. In this article we will discuss facebook integration with java application.








ການ​ນໍາ​ສະ​ເຫນີ: Websites are become an integral part of any business, be it small, medium or big. The web presence is an important aspect to reach the global audience. But only the website is not sufficient to reach the mass audience. For that the social media revolution is very important and it has a cascading effect. Now the social networking sites are also interested to get integrated with other websites as they also need to reach the mass audience. And in the similar way websites need the help of social sites to reach target audience. So, to complement this social sites have exposed their APIs and allows the websites to communicate with them. Facebook provides convenient Graph API to integrate with their site. The Graph API is very powerful and exposes all necessary features required.

The following picture shows the facebook integration with different types of medium like web, mobile, desktop and many other devices.

Facebook integration cycle

Image1: Facebook integration cycle

ຮູບແບບຄໍາຮ້ອງສະຫມັກເຟສບຸກ: Now we need to understand the facebook application model to understanding the integration part. The proxy server model used by facebook is the main integration point with the application. Facebook uses REST web services to open its platform to the developers. Developers are free to use APIs to integrate facebook features in their application. And the most important thing is that the developers get the freedom to select any technology to develop their application. So the application development platform can be anything and facebook APIs are there to support it. And the developer should deploy their application in a way so that it can handle huge volume of data everyday.

Working steps in facebook proxy model: Following are the steps used in facebook proxy model

1) Your web application will reside in your web/application server and you will register the base URL in facebook account

2) When your application is visited in facebook, it will call your registered URL on your server

3) Now, your application will call necessary facebook APIs to get information

4) Your application will use its own database data and facebook data and render it

5) After this facebook returns your application’s output to the user








Steps to create facebook account: Before we start integrating our application with facebook APIs, we need to register our application (website) in facebook. Once you register your web application in facebook, access will be provided to the APIs. Please follow the steps below to register your application.

  • Create your website which will be using facebook APIs
  • Create facebook account and register your application there. Here it will be the website name.
  • After this facebook will generate a client id and secret key for the registered web application.
  • Use callback URL during registration. Facebook will use this URL to redirect the response message

ວິທີການເພື່ອໃຫ້ໄດ້ຮັບຫ້ອງສະຫມຸດເຟສບຸກ:

ກ່ອນທີ່ພວກເຮົາເຊື່ອມໂຍງກັບເຟສບຸກມີຄໍາຮ້ອງສະຫມັກ java ພວກເຮົາພວກເຮົາຕ້ອງການຫ້ອງສະຫມຸດພາກສ່ວນທີສາມບາງ. These libraries will help us communicate with facebook. Different groups of independent java developers have made efficient facebook libraries for integration purpose.

ທ່ານສາມາດກວດສອບ http://code.google.com/p/facebook-java-api/ to get details. These libraries are compatible with Java SE 5 ແລະຂ້າງເທິງ.

Download the following JARS

  • http://facebook-java-api.googlecode.com/files/facebook-java-api-1.7.2.jar
  • http://facebook-java-api.googlecode.com/files/json-1.0.jar
  • http://facebook-java-api.googlecode.com/files/facebook-util-1.7.2.jar

ຖ້າຫາກວ່າທ່ານກໍາລັງໃຊ້ເຄື່ອງແມ່ຂ່າຍຂອງຄໍາຮ້ອງສະຫມັກ Java ເທິງສຸດຂອງ Java SE 5, then you need to download the following JARs

  • http://facebook-java-api.googlecode.com/files/jaxb-api-2.1.jar
  • http://facebook-java-api.googlecode.com/files/jaxb-impl-2.1.jar
  • http://facebook-java-api.googlecode.com/files/jsr173-api-1.0.jar

You do not need the above three JARs if you are using Java SE 6 ຫຼືຫຼັງຈາກນັ້ນ.

ຫລັງຈາກດາວໂຫລດ jars ເຫຼົ່ານີ້, you need to incorporate them in your application. These JARs will provide you API access for different purpose.








Following sample code shows the integration part.

Listing1: Sample showing integration of facebook API and java

[ລະ​ຫັດ]

package techalpine.com;

java.io.BufferedWriter ການນໍາເຂົ້າ;

ການນໍາເຂົ້າ java.io.File;

java.io.FileWriter ການນໍາເຂົ້າ;

java.io.IOException ການນໍາເຂົ້າ;

import net.sf.json.JSONArray;

ການນໍາເຂົ້າ net.sf.json.JSONObject;

ການນໍາເຂົ້າ facebook4j.Facebook;

facebook4j.FacebookException ນໍາເຂົ້າ;

ການນໍາເຂົ້າ facebook4j.FacebookFactory;

ການນໍາເຂົ້າ facebook4j.Post;

ການນໍາເຂົ້າ facebook4j.ResponseList;

facebook4j.conf.Configuration ນໍາເຂົ້າ;

ການນໍາເຂົ້າ facebook4j.conf.ConfigurationBuilder;

public class FacebookImpl {

ສາ​ທາ​ລະ​ນະ void ຕົ້ນ​ຕໍ static(ຊ່ອຍ​ແນ່[] args) ຖິ້ມ FacebookException

{

// Make the configuration builder

ConfigurationBuilder confBuilder = new ConfigurationBuilder();

confBuilder.setDebugEnabled(ຄວາມ​ຈິງ);

// Set application id, secret key and access token

confBuilder.setOAuthAppId(“8282887”);

confBuilder.setOAuthAppSecret(“c3jhdhjhuh”);

confBuilder.setOAuthAccessToken(“2jskjdbjbdjb”);

// Set permission

confBuilder.setOAuthPermissions(“email,publish_stream, id, ຊື່, ຊື່​ແທ້, ນາມ​ສະ​ກຸນ, generic”);

confBuilder.setUseSSL(ຄວາມ​ຈິງ);

confBuilder.setJSONStoreEnabled(ຄວາມ​ຈິງ);

// Create configuration object

Configuration configuration = confBuilder.build();

// Create facebook instance

FacebookFactory ff = FacebookFactory ໃຫມ່(ການຕັ້ງຄ່າ);

ເຟສບຸກເຟສບຸກ = ff.getInstance();

ພະຍາຍາມ {

// Get facebook posts

String results = getFacebookPostes(ເຟສບຸກ);

String responce = stringToJson(ຜົນການຄົ້ນຫາ);

// Create file and write to the file

ເອກະສານເອກະສານ = File ໃຫມ່(“C:\\Facebook\\File\\test.txt”);

ຖ້າຫາກວ່າ (!file.exists())

{

file.createNewFile();

FileWriter fw = FileWriter ໃຫມ່(file.getAbsoluteFile());

BufferedWriter BW = BufferedWriter ໃຫມ່(fw);

bw.write(ຜົນການຄົ້ນຫາ);

bw.close();

System.out.println(“Writing complete”);

}

} ຈັບ (ອີ IOException) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

ສາທາລະນະ getFacebookPostes String static(ເຟສບຸກເຟສບຸກ) ຖິ້ມ FacebookException {

// Get posts for a particular search

ResponseList<Post> results = facebook.getPosts(“Reebok”);

return results.toString();

}

ສາທາລະນະ stringToJson String static(ຂໍ້ມູນ string)

{

// Create JSON object

JSONObject JSONObject = JSONObject.fromObject(data);

JSONArray message = (JSONArray) jsonObject.get(“message”);

System.out.println(“ຂໍ້ຄວາມ : “+message);

return “Done”;

}

}

[/ລະ​ຫັດ]

Facebook Markup Language (FBML): Apart from using facebook REST web service there are another option available to the developers. Facebook Markup Language (FBML) is another powerful offering from facebook community to support application development. Using this markup language developer can easily integrate their application without putting much effort.








Following is a sample code

Listing2: Sample code showing FBML

[ລະ​ຫັດ]

<%@ page language=”java”

contentType=”text/html; charset=UTF-8″

pageEncoding=”UTF-8″

%>

<strong>Myfacebook test page/strong>

<div>

<fb:profile-pic uid=”logged_in_user”

size=”medium”

linked=”ຄວາມ​ຈິງ” /><br>

<fb:name uid=”logged_in_user”

use_you=”false”

linked=”ຄວາມ​ຈິງ”

capitalize=”ຄວາມ​ຈິງ” />,

Use the most popular facebook apps

</div>

[/ລະ​ຫັດ]

Apart from FBML, there are other utilities available for Ajax like effects. There is facebook query language (FBQL) which can be used for searching. All these can be used together and it will help us to make a complex application as per our requirement.

ສະ​ຫຼຸບ: There are many social networking websites available but facebook is most popular. And we have also seen that there are many different applications built on top of facebook APIs. There are also third party libraries available which can be used by java applications to interact with facebook. ດັ່ງນັ້ນທີ່ຈະສະຫຼຸບການສົນທະນາຂອງພວກເຮົາ, we can say that social media integration is a new dimension in developer’s world and we should explore it as much as possible.

 

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share