All Projects → roundrop → Facebook4j

roundrop / Facebook4j

Licence: other
A most easily usable Facebook API wrapper in Java.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Facebook4j

facebook-nodejs
Nodejs module for Facebook api
Stars: ✭ 26 (-91.61%)
Mutual labels:  facebook-api
Facebook-Login-Xamarin-Forms
Demo for login to Facebook API from Xamarin Forms
Stars: ✭ 41 (-86.77%)
Mutual labels:  facebook-api
lambda-facebook-oauth
An AWS Lambda function to facilitate Oauth2 social login with Facebook
Stars: ✭ 16 (-94.84%)
Mutual labels:  facebook-api
PHP-SDK-3.0---Graph-API-base-Facebook-Connect-Tutorial-Source
PHP SDK 3.0 Graph API base Facebook Connect Library
Stars: ✭ 60 (-80.65%)
Mutual labels:  facebook-api
facebook-messenger
Go (GoLang) package for Facebook Messenger API and Chat bot
Stars: ✭ 62 (-80%)
Mutual labels:  facebook-api
facebook-login-for-robots
Facebook Login for 🤖 robots
Stars: ✭ 41 (-86.77%)
Mutual labels:  facebook-api
Facebooktoolkit
a tool to get Facebook data, and some Facebook bots, and extra tools found on Facebook Toolkit ++.
Stars: ✭ 227 (-26.77%)
Mutual labels:  facebook-api
facebook-api-video-upload
👌 A handy function to upload video in chunk on the facebook grap.
Stars: ✭ 48 (-84.52%)
Mutual labels:  facebook-api
facebook-node-sdk
Modeled from the (Facebook Javascript SDK), now with the facebook-node-sdk you can now easily write the same code and share between your server (nodejs) and the client (Facebook Javascript SDK).
Stars: ✭ 519 (+67.42%)
Mutual labels:  facebook-api
facebook-go-sdk
A very simple and easy-to-use Facebook SDK for Golang.
Stars: ✭ 18 (-94.19%)
Mutual labels:  facebook-api
node-social-feed-api
Aggregates social media feeds and outputs them to use in an API
Stars: ✭ 20 (-93.55%)
Mutual labels:  facebook-api
facebook-bot-autoresponder
Facebook bot that automatically responds to the comments of a certain post
Stars: ✭ 90 (-70.97%)
Mutual labels:  facebook-api
GraphiPy
GraphiPy: Universal Social Data Extractor
Stars: ✭ 61 (-80.32%)
Mutual labels:  facebook-api
FacebookMessengerActivity
Share with Facebook Messenger App.
Stars: ✭ 16 (-94.84%)
Mutual labels:  facebook-api
messenger
💬 A PHP library for Facebook Messenger
Stars: ✭ 53 (-82.9%)
Mutual labels:  facebook-api
social-post-api
Social Media API: Automate Posting and Analytics to Social Networks like Instagram, TikTok, Twitter, Facebook, LinkedIn, Reddit, YouTube, and Telegram
Stars: ✭ 38 (-87.74%)
Mutual labels:  facebook-api
Facebook-Auto-Pilot
Automate common Facebook activities such as posting to groups and pages walls. Effortlessly post to multiple groups or pages.
Stars: ✭ 126 (-59.35%)
Mutual labels:  facebook-api
Multistreamer
[discontinued] A webapp for publishing video to multiple streaming services at once.
Stars: ✭ 281 (-9.35%)
Mutual labels:  facebook-api
facebook-py-sdk
Facebook Python SDK
Stars: ✭ 15 (-95.16%)
Mutual labels:  facebook-api
facebook-ruby-ads-sdk
The Facebook Marketing API in Ruby.
Stars: ✭ 59 (-80.97%)
Mutual labels:  facebook-api

Facebook4J Build Status

Facebook4J is a Facebook Graph API binding library for the Java language licensed under Apache License 2.0.

Version

2.4.13

Install

<dependency>
  <groupId>org.facebook4j</groupId>
  <artifactId>facebook4j-core</artifactId>
  <version>[2.4,)</version>
</dependency>

Code Examples

Please see https://facebook4j.github.io/en/code-examples.html for complete documentation.

Getting Facebook Instance

At first it is necessary to acquire Facebook instance to use Facebook4J.
You can get Facebook instance in FacebookFactory.getInstance().

Facebook facebook = new FacebookFactory().getInstance();

If App ID / App Secret / access token / access permission are listed in facebook4j.properties then, they are set in Facebook instance given back.
See Configuration | Facebook4J - A Java library for the Facebook Graph API for the detail.
When they are not listed, it is setable later as follows:

facebook.setOAuthAppId(appId, appSecret);
facebook.setOAuthPermissions(commaSeparetedPermissions);
facebook.setOAuthAccessToken(new AccessToken(accessToken, null));

OAuth support

Getting User Access Token

It is possible to authenticate users using Facebook accounts with your web application.
An example implementation is available at https://github.com/roundrop/facebook4j-oauth-example .

Getting App Access Token

You can get App Access Token via Facebook.getOAuthAppAccessToken() method.

facebook.getOAuthAppAccessToken();

Getting Page Access Token

You can get Page Access Token as below:

ResponseList<Account> accounts = facebook.getAccounts();
Account yourPageAccount = accounts.get(0);  // if index 0 is your page account.
String pageAccessToken = yourPageAccount.getAccessToken();

Getting Device Access Token

With Facebook Login for Devices people can easily and safely log into your apps and services with their Facebook account on devices with limited input or display capabilities.
(See Facebook's Documentation: Facebook Login for Devices )
An example implementation is available at https://github.com/roundrop/facebook4j-oauth-example .

Extending expiration of an Access Token

(See Facebook's Documentation: Expiration and Extension of Access Tokens
You can extend Access Token's expiration as below:

String shortLivedToken = "your-short-lived-token";
AccessToken extendedToken = facebook.extendTokenExpiration(shortLivedToken);

Publishing a message

You can publish a message via Facebook.postStatusMessage() method.

facebook.postStatusMessage("Hello World from Facebook4J.");

Publishing a link

You can publish a link via Facebook.postFeed() method.

PostUpdate post = new PostUpdate(new URL("https://facebook4j.github.io"))
                    .picture(new URL("https://facebook4j.github.io/images/hero.png"))
                    .name("Facebook4J - A Java library for the Facebook Graph API")
                    .caption("facebook4j.org")
                    .description("Facebook4J is a Java library for the Facebook Graph API.");
facebook.postFeed(post);

Facebook.postLink() method is simple way to post.

facebook.postLink(new URL("https://facebook4j.github.io"));
facebook.postLink(new URL("https://facebook4j.github.io"), "A Java library for the Facebook Graph API");

Getting News Feed

Facebook.getHome() returns a List of user's latest News Feed.

ResponseList<Post> feed = facebook.getHome();

Like

You can like a Post, Photo, ... via Facebook.like****() methods.

facebook.likePost(postId);

Also, You can unlike a Post, Photo, ... via Facebook.unlike****() methods.

facebook.unlikePost(postId);

Publising a comment

You can comment a Post, Photo, ... via Facebook.comment****() methods.

facebook.commentPhoto(photoId, "It's a nice photo!");

Searching

You can search for Posts, Users, ... via Facebook.search****() methods.

Search for public Posts

ResponseList<Post> results = facebook.searchPosts("watermelon");

Search for Users

ResponseList<User> results = facebook.searchUsers("mark");

Search for Events

ResponseList<Event> results = facebook.searchEvents("conference");

Search for Groups

ResponseList<Group> results = facebook.searchGroups("programming");

Search for Places

// Search by name
ResponseList<Place> results = facebook.searchPlaces("coffee");

// You can narrow your search to a specific location and distance
GeoLocation center = new GeoLocation(37.76, -122.4.8);
int distance = 1000;
ResponseList<Place> searchPlaces("coffee", center, distance);

Search for Checkins

// you or your friend's latest checkins, or checkins where you or your friends have been tagged
ResponseList<Checkin> results = facebook.searchCheckins();

Search for Locations

// To search for objects near a geographical location
GeoLocation center = new GeoLocation(37.76, -122.4.8);
int distance = 1000;
ResponseList<Location> searchLocations(center, distance);

// To search for objects at a particular place
String placeId = "166793820034304";
ResponseList<Location> locations = facebookBestFriend1.searchLocations(placeId);

Executing FQL

You can execute FQL via Facebook.executeFQL() method.
Also you can execute multiple FQL in one call via Facebook.executeMultiFQL() method.

// Single FQL
String query = "SELECT uid2 FROM friend WHERE uid1=me()";
JSONArray jsonArray = facebook.executeFQL(query);
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    System.out.println(jsonObject.get("uid2"));
}

// Multiple FQL
Map<String, String> queries = new HashMap<String, String>();
queries.put("all friends", "SELECT uid2 FROM friend WHERE uid1=me()");
queries.put("my name", "SELECT name FROM user WHERE uid=me()");
Map<String, JSONArray> result = facebook.executeMultiFQL(queries);
JSONArray allFriendsJSONArray = result.get("all friends");
for (int i = 0; i < allFriendsJSONArray.length(); i++) {
    JSONObject jsonObject = allFriendsJSONArray.getJSONObject(i);
    System.out.println(jsonObject.get("uid2"));
}
JSONArray myNameJSONArray = result.get("my name");
System.out.println(myNameJSONArray.getJSONObject(0).get("name"));

Executing Batch Requests

You can execute Batch Requests via Facebook.executeBatch() method.

// Executing "me" and "me/friends?limit=50" endpoints
BatchRequests<BatchRequest> batch = new BatchRequests<BatchRequest>();
batch.add(new BatchRequest(RequestMethod.GET, "me"));
batch.add(new BatchRequest(RequestMethod.GET, "me/friends?limit=50"));
List<BatchResponse> results = facebook.executeBatch(batch);

BatchResponse result1 = results.get(0);
BatchResponse result2 = results.get(1);

// You can get http status code or headers
int statusCode1 = result1.getStatusCode();
String contentType = result1.getResponseHeader("Content-Type");

// You can get body content via as****() method
String jsonString = result1.asString();
JSONObject jsonObject = result1.asJSONObject();
ResponseList<JSONObject> responseList = result2.asResponseList();

// You can map json to java object using DataObjectFactory#create****()
User user = DataObjectFactory.createUser(jsonString);
Friend friend1 = DataObjectFactory.createFriend(responseList.get(0).toString());
Friend friend2 = DataObjectFactory.createFriend(responseList.get(1).toString());
:

You can attach a binary data to batch request as follows:

BatchRequests<BatchRequest> batch = new BatchRequests<BatchRequest>();
Media file = new Media(new File("...image.png"));
BatchAttachment attachment = new BatchAttachment("file", file);
batch.add(new BatchRequest(RequestMethod.POST, "me/photos")
              .body("message=My photo")
              .attachedFile(attachment));

Executing Raw API (setting the endpoint on your own)

You can execute the API endpoint that you want to run via Facebook.call****() method.

// GET
RawAPIResponse res = facebook.callGetAPI("me");
JSONObject jsonObject = actual.asJSONObject();
String id = jsonObject.getString("id");

// POST
Map<String, String> params = new HashMap<String, String>();
params.put("message", "hello");
RawAPIResponse res = facebook.callPostAPI("me/feed", params);

// DELETE
RawAPIResponse res = facebook.callDeleteAPI("123456/likes");
if (res.isBoolean()) {
  System.out.println(res.asBoolean());
}

You can execute the API endpoint that is not supported by Facebook4J via Facebook.call****() method.


Reading options

You can set various reading options to the method that Reading object includes in arguments.

Selecting specific fields

You can choose the fields you want returned via Reading.fields("fieldName1,fieldName2,...") .

// Getting user's email address only
User user = facebook1.getUser(id1.getId(), new Reading().fields("email"));

limit/offset

// Getting 1st-10th results
ResponseList<Post> results = facebook.searchPosts("watermelon", new Reading().limit(10));

// Getting 11th-20th results
ResponseList<Post> results = facebook.searchPosts("watermelon", new Reading().limit(10).offset(10));

until/since

until/since values can be a unix timestamp or any date accepted by PHP's strtotime format.

ResponseList<Post> results = facebook.searchPosts("watermelon", new Reading().until("yesterday"));

Pagination

You can get next/previous page with Paging object in results via Facebook.fetchNext() / Facebook.fetchPrevious() methods.

ResponseList<Option> page1 = facebook.getQuestionOptions(questionId);

// Getting Next page
Paging<Option> paging1 = page1.getPaging();
ResponseList<Option> page2 = facebook.fetchNext(paging1);

// Getting Previous page
Paging<Option> paging2 = page2.getPaging();
page1 = facebook.fetchPrevious(paging2);

Official Web Site

see: https://facebook4j.github.io

License

Facebook4J is released under Apache License 2.0.

Facebook4J includes software from Twitter4J to handle HTTP request/response and greatly internal logic. You can see the license term at http://twitter4j.org/en/index.html#license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].