Steps to integrate Facebook and Twitter with java application?

 

Social Media Integration

Social Media Integration

Tổng quan: Social media revolution is become a significant event in many areas like marketing, networking, analytics and many more. The exposure to social media like facebook, twitter, linked-in has a tremendous impact on business. Social media is like a natural resource for collecting user feedback, comments, suggestions etc. So the integration of social media with applications are also very important. These applications will be able to connect to these medias and get information. In this article I will discuss the integration of facebook and twitter with java application.

Giới thiệu: In the modern era websites have become an integral part of any business, be it big, medium or small. The web presence is an important aspect to reach to the global audience. Presence of a business on the website is not enough to reach the greater mass audience. To reach out to a larger mass social media plays a significant role as it has a cascading and concurrent effect. Now the social networking and media sites are also interested to integrate and communicate with other websites as they also need to reach the mass common audience. In this similar way websites need the help of social sites to reach target audience. So, in order to complement this, social media sites have exposed their APIs and allows the websites to communicate and integrate with them. Facebook provides convenient APIs (known as Graph API) to integrate with their site and communicate. The Graph API is very powerful and flexible. It exposes all necessary features (known as interfaces) required for integration.

Facebook application model: Before going into the actual implementation let’s understand the facebook application model. Facebook opens its platform to the developers using the REST web services. Là một nhà phát triển, chúng tôi được tự do sử dụng các API của sự lựa chọn của chúng tôi để tích hợp các tính năng của facebook trong ứng dụng của chúng tôi. Cũng như là một nhà phát triển, chúng tôi được tự do sử dụng công nghệ của sự lựa chọn. Facebook sử dụng mô hình máy chủ proxy là một điểm hội nhập chính. Các máy chủ facebook Proxy sau các bước sau đây:

  • Các ứng dụng web sẽ nằm trong web máy chủ ứng dụng / của chúng tôi và chúng tôi cần phải đăng ký URL cơ sở trong tài khoản facebook.
  • Khi ứng dụng được truy cập trong facebook, nó sẽ gọi các URL đã đăng ký trên máy chủ ứng dụng.
  • Now, ứng dụng sẽ gọi các API facebook cần thiết để có được những thông tin có liên quan.
  • ứng dụng của chúng tôi sử dụng dữ liệu và cơ sở dữ liệu facebook riêng của mình dữ liệu và làm cho nó
  • Sau khi facebook này trả về đầu ra ứng dụng của chúng tôi cho người sử dụng

Làm thế nào để có được facebook thư viện:

Before we integrate facebook with our java application we need some third party libraries. These third party libraries will help us in integration and communication with facebook (actually accessing facebook application installed in their server). Different independent groups of java and open source developers have made efficient facebook libraries for integration purpose.

You can check http://code.google.com/p/facebook-java-api/ to get more details about the APIs and Jars. These third party libraries are compatible with Java SE 5 and above.

Download the following JAR files

If you are using Java application server on top of Java SE 5, then you should download the following JAR files:

Ba lọ ở trên không cần thiết nếu bạn đang sử dụng Java SE 6 hoặc sau đó.

Sau khi tải về các lọ, bạn cần phải kết hợp chúng trong web của bạn hoặc đứng một mình ứng dụng. Những lọ được thực hiện để cung cấp truy cập API để các ứng dụng khách hàng cho mục đích khác nhau.

Ví dụ sau sẽ hiển thị một phần tích hợp:

Listing 1: mã Java mẫu cho FaceBook hội nhập

gói com.home.social;

nhập khẩu java.io.BufferedWriter;

nhập khẩu java.io.File;

nhập khẩu java.io.FileWriter;

nhập khẩu java.io.IOException;

nhập khẩu java.text.SimpleDateFormat;

nhập khẩu java.util.Date;

nhập khẩu net.sf.json.JSONObject;

nhập khẩu net.sf.json.JsonConfig;

nhập khẩu facebook4j.Facebook;

facebook4j.FacebookException nhập khẩu;

nhập khẩu facebook4j.FacebookFactory;

nhập khẩu facebook4j.Post;

nhập khẩu facebook4j.ResponseList;

facebook4j.conf.Configuration nhập khẩu;

nhập khẩu facebook4j.conf.ConfigurationBuilder;

public class FacebookIntegration {

public static void main(Chuỗi[] args) throws FacebookException {

// Create conf builder and set authorization and access keys

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

configurationBuilder.setDebugEnabled(đúng);

configurationBuilder.setOAuthAppId(“xxxx”);

configurationBuilder.setOAuthAppSecret(“xxxxx”);

configurationBuilder.setOAuthAccessToken(“xxxx”);

configurationBuilder

.setOAuthPermissions(“email, publish_stream, id, tên, first_name, last_name, read_stream , generic”);

configurationBuilder.setUseSSL(đúng);

configurationBuilder.setJSONStoreEnabled(đúng);

 

// Create configuration and get facebook instance

Configuration configuration = configurationBuilder.build();

FacebookFactory ff = new FacebookFactory(cấu hình);

Facebook facebook = ff.getInstance();

thử {

// Set search string and get results

String searchPost = “MACDonaldsIndia”;

Date date = new Date();

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(

“dd_MM_yyyy_hh_mm”);

String fileName = “D:\\FacebookConfigFolder\\File\\” + searchPost

+ “_” + simpleDateFormat.format(date) + “.txt”;

String results = getFacebookPostes(Facebook, searchPost);

tập tin File = mới File(fileName);

nếu (!File tồn tại()) {

file.createNewFile();

FileWriter fw = new FileWriter(file.getAbsoluteFile());

BufferedWriter bw = new BufferedWriter(fw);

bw.write(results);

bw.close();

System.out.println(“Đã hoàn thành”);

}

} bắt (IOException e) {

e.printStackTrace();

}

}

// Phương pháp này được sử dụng để có được bài viết facebook dựa trên các thiết lập chuỗi tìm kiếm

// above

công getFacebookPostes static String(Facebook facebook, chuỗi searchPost)

throws FacebookException {

Chuỗi SearchResult = “Mục : ” + searchPost + “\n”;

StringBuffer searchMessage = new StringBuffer();

ResponseList<Bài đăng> kết quả = facebook.getPosts(searchPost);

cho (thư bưu chính : results) {

System.out.println(post.getMessage());

searchMessage.append(post.getMessage() + “\n”);

cho (int j = 0; j < post.getComments().size(); j ) {

searchMessage.append(post.getComments().get(j).nhận được từ()

.getName()

+ “, “);

searchMessage.append(post.getComments().get(j).getMessage()

+ “, “);

searchMessage.append(post.getComments().get(j).getCreatedTime()

+ “, “);

searchMessage.append(post.getComments().get(j).getLikeCount()

+ “\n”);

}

}

Chuỗi feedString = getFacebookFeed(Facebook, searchPost);

SearchResult = SearchResult + searchMessage.toString();

SearchResult = SearchResult + feedString;

trở SearchResult;

}

// Phương pháp này được sử dụng để có được facebook thức ăn dựa trên các thiết lập chuỗi tìm kiếm

// above

public static String getFacebookFeed(Facebook facebook, chuỗi searchPost)

throws FacebookException {

Chuỗi SearchResult = “”;

StringBuffer searchMessage = new StringBuffer();

ResponseList<Bài đăng> kết quả = facebook.getFeed(searchPost);

cho (thư bưu chính : results) {

System.out.println(post.getMessage());

searchMessage.append(post.getFrom().getName() + “, “);

searchMessage.append(post.getMessage() + “, “);

searchMessage.append(post.getCreatedTime() + “\n”);

}

SearchResult = SearchResult + searchMessage.toString();

trở SearchResult;

}

// This method is used to create JSON object from data string

public static String stringToJson(String data) {

JsonConfig cfg = new JsonConfig();

thử {

JSONObject jsonObject = JSONObject.fromObject(data, cfg);

System.out.println(“JSON = ” + jsonObject.toString());

} bắt (Exception e) {

e.printStackTrace();

}

return “JSON Created”;

}

}

Integrating with Twitter: In order to integrate you application with twitter we need to use the library – twitter4j. This is a well documented library that helps any java developer to integrate his/her application with twitter. As a developer we need to follow certain steps as mentioned below:

  • Send a request to twitter asking for a token. This request should carry both consumer key and a secret key.
  • Store the response received from twitter.
  • Once the response is received, the authentication URL is extracted from the response.
  • The user then needs to be redirected to the authentication URL, so that he can sign in.
  • User signs in and gets a Personal Identification Number or PIN.
  • User then enters PIN in the application.
  • Once the PIN is entered the application should ask Twitter for a security token, providing consumer parameters like above as well as the previously stored request token and the PIN.
  • Once the token is received, every request going o twitter should have this token along with the PIN

Listing 2: Sample Java code for Twitter integration

gói com.home.social;

nhập khẩu java.io.BufferedWriter;

nhập khẩu java.io.FileWriter;

import java.util.List;

import twitter4j.Query;

import twitter4j.QueryResult;

import twitter4j.Status;

import twitter4j.Twitter;

import twitter4j.TwitterFactory;

import twitter4j.conf.ConfigurationBuilder;

public class TwitterIntegration {

public static void main(Chuỗi[] args) throws Exception{

// Create configuration builder and set key, token etc

ConfigurationBuilder cb = new ConfigurationBuilder();

cb.setOAuthConsumerKey(“xxx”);

cb.setOAuthConsumerSecret(“xxxx”);

cb.setOAuthAccessToken(“xxxxx”);

cb.setOAuthAccessTokenSecret(“xxxx”);

// Create twitter instance

Twitter twitter = new TwitterFactory(cb.build()).getInstance();

// Create file writer and buffer writer

FileWriter fstream = new FileWriter(“twitterstream.txt”,đúng);

BufferedWriter ra = new BufferedWriter(fstream);

// Create Query object and set search string

Query query = new Query(“”);

query.setQuery(“#USAirways”);

// Get query result

QueryResult qr = twitter.search(query);

// Get tweets and write in the file

trong khi(qr.hasNext()){

qr.nextQuery();

List<Status> tweets = qr.getTweets();

cho (Status t: tweets){

System.out.println(t.getId() + ” – ” + t.getCreatedAt() + “: ” + t.getText());

out.write(“\n”+t.getId()+”,”);

out.write(“\t”+t.getText()+”,”);

out.write(“\t”+t.getUser()+”,”);

}

}

thử{

Thread.sleep(1000*60*15);

}bắt(Exception e) {}

}

}

Kết luận: There are many social networking websites available. Out of these facebook and twitter are more commonly used. Java provides libraries to integrate our applications with these websites. So to conclude our discussion, we can understand that social media integration is a new dimension in developer’s world and we should explore it as much as possible.

============================================= ============================================== Mua sách Techalpine tốt nhất trên Amazon,en,Thợ điện CT Hạt dẻ,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Thưởng thức blog này,,en,làm ơn mở rộng vốn từ,,en,techalpine.com/apache-mahout-and-machine-learning,,en? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share