List Facebook friends and groups post to their walls with graph API in JSF/PrimeFaces the simple way

Before we continue please make sure you already have a Facebook App and an access_token. Please visit here on how to get an access_token from your JSF application with the necessary access permissions. Please visit here to learn more about Facebook access permissions.

To make everything easy and simple we use restfb (get it here). It is an encapsulation of most of Facebook API. Aside from just posting to walls we also make sure that we include an action link. An additional link back to your site. The action link is located beside the standard "like" and "comment" links of a post similar to the "Get Bonus" from the graphic below.

List Facebook friends and groups post to their 

walls with graph API in JSF/PrimeFaces the simple way

Here is the important part of a method in our backing bean. It should work with PrimeFaces p:dataGrid. Please note that we are using Facebook Graph. We post direct without the Facebook dialog prompt. Let us assume the containing method of the snippet below is called getFacebook() returning List.


  .
  .
  .
  
  List list = new ArrayList ();

   try {

      // Link back to your site from a Facebook wall post.  
      // Must be from domain of Facebook app.

      String actionLink = 
            "http://your-domain.com/your-app/Promo.xhtml";

      // scope should be in the permission 
      // of your access_token.  

      actionLink = 
            "http://www.facebook.com/dialog/oauth/?
                 client_id=YOUR-APP-ID&"
            + "redirect_uri=" + actionLink + "&"
            + "scope=publish_stream,user_groups,status_update&"
            + "response_type=token";

      // actions in Facebook is a JSON string and should 
      // URL encoded. 

      Map map = new HashMap();
      map.put("name", "Buy Now");
      map.put("link", actionLink);

      DefaultJsonMapper obj = new DefaultJsonMapper();
      String actions = obj.toJson(map);
      actions = java.net.URLEncoder.encode(actions, "UTF-8");


      // We use restfb in communicating with Facebook

      FacebookClient facebookClient = 
                new DefaultFacebookClient(token);

      // Groups

      com.restfb.Connection<Group> gr = 
         facebookClient.fetchConnection
             ("me/groups", Group.class);

      List<Group> g = gr.getData();
      int len = g.size();

      for (int i = 0; i < len; i++) {

          Group G = g.get(i);
          String groupName = G.getName();
          String groupId = G.getId();
          String groupPicture = 
               "http://graph.facebook.com/" + groupId+ "/picture";
             
	   Info info = new Info();

           info.setGroupName (groupName);
	   info.setGroupId (groupId);
           info.setGroupPicture (groupPicture); 
           
           info.setToken(access_token);
           info.setLink("LINK-YOU-ARE-POSING");
           info.setPicture("PICTURE-THAT-GOES-WITH-POST");
           info.setDescription("DESC-OF-POST");
           info.setName("NAME-OF-POST");
           info.setActions(actions);
           info.setId(id);

           list.add(info);

           /*
              Scroll down below on how to use the Facebook
              Feed dialog instead of graph.
           */

      }

      // Access your friends

      com.restfb.Connection<User> my = 
           facebookClient.fetchConnection
           ("me/friends", User.class);
      List<User> u = my.getData();

      // Do same as with groups above, 
      //  loop through to update list wit Info objects

    } catch (Exception e) {
      
      // 

    } 

    return list;

    .
    .
    .
     

Below is how the p:dataGrid should look like


<p:dataGrid paginator="false" style="width:850px;"
    var="bff" value="#{yourBean.facebook}" 
    columns="6">

    <p:column>

       <h:commandLink 
             actionListener="#{yourBean.publish}">
           <h:panelGrid >
              <h:graphicImage value ="#{bff.groupPicture}" />
              <h:outputText value="#{bff.groupName}" 
                style="font-size:10px;font-weight: bold" />
           </h:panelGrid>   

           <f:param name="profileid" value="#{bff.groupId}" />
           <f:param name="token" value="#{bff.token}" />
           <f:param name="url" value="#{bff.link}" />
           <f:param name="picture" value="#{bff.picture}" />
           <f:param name="desc" value="#{bff.description}" />
           <f:param name="name" value="#{bff.name}" />
           <f:param name="actions" value="#{bff.actions}" />

       </h:commandLink>
            
    </p:column>

</p:dataGrid>


Below is the Info class and the publish method. Please note that the publish method is part of our bean not of the Info class.


 public class Info {
     private String groupName;
     private String groupId;

     public String getGroupName () {
          return groupName;
     }

     public void setGroupName (String s) {
        groupName = s;
     }

     .
     .
     .

 }


     public void publish (Action event) {

        FacesContext context = FacesContext.getCurrentInstance();
        Map rmap = 
             context.getExternalContext().getRequestParameterMap();
        String token = (String) rmap.get("token");
        String id = (String) rmap.get("profileid");
        String name = (String) rmap.get("name");
        String desc = (String) rmap.get("desc");
        String link = (String) rmap.get("url");
        String picture = (String) rmap.get("picture");
        String actions = (String) rmap.get("actions");

        FacebookClient fbc = new DefaultFacebookClient(token_);

        FacebookType response =
           fbc.publish(id_ + "/feed", FacebookType.class,
           Parameter.with("app_id", "YOUR-APP-ID"),
           Parameter.with("access_token", token),
           Parameter.with("link", link),
           Parameter.with("picture", picture),
           Parameter.with("description", desc),
           Parameter.with("name", name),
           Parameter.with("actions", actions),
           Parameter.with("message", " ")); 

        String mess = response.toString();

        context.addMessage(null, new FacesMessage(mess, ""));

    }          
        

For some reasons that you wanted to post to Facebook through Facebook Feed dialog. Below is how.


    String F = "http://www.facebook.com/dialog/
          feed?app_id=YOUR-APP-ID&"    
    + "redirect_uri=" + YOUR-REDIRECT-URL + "&"
    + "access_token=" + token + "&" 
    + "link=" + url + "&"
    + "picture=" + picture + "&"
    + "description=" + desc + "&"
    + "to=" + id + "&"
    + "name=" + name + "&"
    + "actions=" + actions;
                

Set the string above as return value in a method of our Info class, we call that method set/getClick for the sake of this blog. Below is the p:dataGrid corresponding to the above change.


 <p:dataGrid paginator="false" style="width:850px;"
    var="bff" value="#{yourBean.facebook}" 
    columns="6">

    <p:column>

       <h:outputLink value = "#{bff.click}" >    
           <h:panelGrid >
              <h:graphicImage value ="#{bff.groupPicture}" />
              <h:outputText value="#{bff.groupName}" 
                style="font-size:10px;font-weight: bold" />
           </h:panelGrid>   

       </h:outputLink>
            
    </p:column>

 </p:dataGrid>


Good luck.

If you like the article, please share.
(Site URL pattern has changed as a result social actions counter was reset.)



Comment icon Comments (Newest first)