Coder Social home page Coder Social logo

shkelqimbehluli / restfb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from restfb/restfb

0.0 1.0 0.0 10.6 MB

RestFB is a simple and flexible Facebook Graph API client written in Java.

Home Page: http://restfb.com

License: MIT License

Shell 0.07% Java 99.93%

restfb's Introduction

Stories in Ready Build Status Join the chat at https://gitter.im/restfb/restfb

What it is

RestFB is a pure Java Facebook Graph API client with no external dependencies.

It was created by Mark Allen and is maintained by Norbert Bartels along with a worldwide team of contributors.

Licensing

RestFB uses other open-source software - see the LICENSE.*.txt files. RestFB itself is open source software released under the terms of the MIT License.

Installation

RestFB is a single JAR - just drop it into your app and you're ready to go. Download it from Maven Central:

Maven Central

Building it Yourself

Just type

ant dist

...and you're done.

Usage examples

The following paragraphs show only a subset of the possibilities you have, if you use RestFB. To get a complete overview you should check the examples in the documentation. The code samples there are commented and have a lot of additional information that are very useful.

Initialization

DefaultFacebookClient is the FacebookClient implementation that ships with RestFB. You can customize it by passing in custom JsonMapper and WebRequestor implementations, or simply write your own FacebookClient instead for maximum control.

FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN, Version.LATEST);

// Get added security by using your app secret:
FacebookClient facebookClient = 
       new DefaultFacebookClient(MY_ACCESS_TOKEN, MY_APP_SECRET, Version.VERSION_2_8);

Fetching Single Objects

see RestFB documentation

For all API calls, you need to tell RestFB how to turn the JSON returned by Facebook into Java objects. In this case, the data we get back should be mapped to the User and Page types, respectively. You can write your own types too!

User user = facebookClient.fetchObject("me", User.class);
Page page = facebookClient.fetchObject("cocacola", Page.class);

out.println("User name: " + user.getName());
out.println("Page likes: " + page.getLikes());

Fetching Connections

see RestFB documentation

Connection is the name of an object list. You'll get a connection if you try to fetch your feed for example. As you can see in this example, you can simple iterate over the elements or access the contained data directly.

Connection<Post> myFeed = facebookClient.fetchConnection("me/feed", Post.class);

out.println("First item in my feed: " + myFeed.getData().get(0));

// Connections support paging and are iterable
for (List<Post> myFeedConnectionPage : myFeed)
  for (Post post : myFeedConnectionPage)
    out.println("Post: " + post);

Passing Parameters

see RestFB documentation

You can pass along any parameters you'd like to the Facebook endpoint.

Date oneWeekAgo = new Date(currentTimeMillis() - 1000L * 60L * 60L * 24L * 7L);

Connection<Post> filteredFeed = facebookClient.fetchConnection("me/feed", Post.class,
  Parameter.with("limit", 3), Parameter.with("until", "yesterday"),
    Parameter.with("since", oneWeekAgo));

out.println("Filtered feed count: " + filteredFeed.getData().size());

Selecting Specific Fields

see RestFB documentation

With Graph API 2.4 you only get a subset of the possible fields prefilled. But you may define which fields you really need.

User user = facebookClient.fetchObject("me", User.class,
  Parameter.with("fields", "id,name,last_name"));

out.println("User name: " + user.getName());

Getting Any Kind of Data as a JSON Object

see RestFB documentation

Sometimes you can't know field names at compile time so the @Facebook annotation can't be used. Or maybe you'd like full control over the data that gets returned. Either way, RestFB has you covered. Just map any API call to JsonObject.

// Here's how to fetch a single object

JsonObject btaylor = facebookClient.fetchObject("btaylor", JsonObject.class);
out.println(btaylor.getString("name"));

// Here's how to fetch a connection

JsonObject photosConnection = facebookClient.fetchObject("me/photos", JsonObject.class);
String firstPhotoUrl = photosConnection.getJsonArray("data").getJsonObject(0).getString("source");
out.println(firstPhotoUrl);

Publishing a Message

see RestFB documentation

// Publishing a simple message.
// FacebookType represents any Facebook Graph Object that has an ID property.

FacebookType publishMessageResponse =
  facebookClient.publish("me/feed", FacebookType.class,
    Parameter.with("message", "RestFB test"));

out.println("Published message ID: " + publishMessageResponse.getId());

Publishing a Photo

see RestFB documentation

// Publishing an image to a photo album is easy!
// Just specify the image you'd like to upload and RestFB will handle it from there.

FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,
  BinaryAttachment.with("cat.png", getClass().getResourceAsStream("/cat.png")),
  Parameter.with("message", "Test cat"));

out.println("Published photo ID: " + publishPhotoResponse.getId());

// Publishing a video works the same way.

facebookClient.publish("me/videos", FacebookType.class,
  BinaryAttachment.with("cat.mov", getClass().getResourceAsStream("/cat.mov")),
  Parameter.with("message", "Test cat"));

Deleting

see RestFB documentation

Boolean deleted = facebookClient.deleteObject("some object ID");
out.println("Deleted object? " + deleted);

Map Your Own Types

see RestFB documentation

public class MyClass {
  @Facebook
  String name;

  @Facebook
  BigDecimal value;

  // If a Facebook field doesn't match your field's name, specify it explicitly

  @Facebook("lots_of_numbers")
  List<Integer> lotsOfNumbers;
  
  // You can annotate methods with @JsonMappingCompleted to perform
  // post-mapping operations.
  //
  // This is useful if you want to massage the data FB returns.
  
  @JsonMappingCompleted
  void allDone(JsonMapper jsonMapper) {   
    if(lotsOfNumbers.size() == 0)
      throw new IllegalStateException("I was expecting more numbers!");
  }
}

restfb's People

Contributors

revetkn avatar nbartels avatar kaosko avatar pietdaniel avatar fkurkowski avatar alexdlaird avatar marcelstoer avatar sbisht avatar johnou avatar gitter-badger avatar tsuijten avatar tzellman avatar percipio-gitdub avatar venilnoronha avatar uglytroll avatar zpalmai avatar cmmoran avatar janschweizer avatar kukido avatar graysky avatar skaluva avatar gelisam avatar mattiasholmqvist avatar waffle-iron avatar maczam avatar kevinleturc avatar egergo avatar fmachado avatar eebbesen avatar bseib avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.