Scenario: I have a web service running on WLS that posts messages to a Facebook page.
Part 1 - The Facebook Client
I use restFB here --> restFB
The client code -
package fb;
import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.Account;
import com.restfb.types.FacebookType;
import com.restfb.types.Page;
import com.restfb.types.Post;
import com.restfb.types.User;
import com.sun.org.apache.bcel.internal.generic.Select;
import java.util.Arrays;
import java.util.List;
public class PostMsg2Page {
public PostMsg2Page() {
super();
}
public String post(String msg) {
try{
String MY_ACCESS_TOKEN =
"GetYourAccessToken as described in the restFB docs";
System.out.println("PostMsg2Page()");
FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN);
System.out.println("PostMsg2Page() FB client got!");
User user = facebookClient.fetchObject("me", User.class);
System.out.println("User name: " + user.getName());
// Add posts to a Page
DefaultFacebookClient fbClient;
Connection myAccounts;
fbClient = new DefaultFacebookClient(MY_ACCESS_TOKEN);
myAccounts = fbClient.fetchConnection("me/accounts", Account.class);
System.out.println("MyAccounts size = " +myAccounts.getData().size());
Account myAccount = new Account();
for (int i =0; i < myAccounts.getData().size(); i++){
myAccount = (Account)myAccounts.getData().get(i);
String accountName = myAccount.getName();
System.out.println("Account name = " + accountName);
if (accountName.equalsIgnoreCase("NiallsTestPage")){
// get the page access token
String pageAccessToken = myAccount.getAccessToken();
fbClient = new DefaultFacebookClient(pageAccessToken);
System.out.println("About to post...");
fbClient.publish("me/feed", FacebookType.class, Parameter.with("message", msg));
}
}
}
catch (Exception e){
e.printStackTrace();
}
return "success";
}
}
Here I essentially connect to my Facebook account.
I then browse thru my pages (accounts) until I find the one I want to post to.
This is, in my case, NiallsTestPage.
I then do the necessary...
Regarding the OAuth token -
Login to https://developers.facebook.com/tools/explorer
to get your own.
I test and see that all is working.
I then jar up this project and add it to my web service project.
The Java web service is as follows -
WebService(endpointInterface = "fbws.ClientPortType")
@WLHttpTransport(serviceUri = "/ClientPort", portName = "ClientPort")
public class Client {
public Client() {
super();
}
public String post2FB(String msg) {
PostMsg2Page pm = new PostMsg2Page();
pm.post(msg);
return "success";
}
}
I deploy to WLS.
I now login to https://developers.facebook.com/tools/explorer again,
this time just to save the Facebook cert locally.
I then add this cert to my cacerts store
Note the location -
I re-start WLS and then test the web service -
I check the Facebook page -
No comments:
Post a Comment