1 package cz.xelfi.feedbook;
3 import com.google.code.facebookapi.FacebookException;
4 import com.google.code.facebookapi.FacebookJaxbRestClient;
5 import com.google.code.facebookapi.Permission;
6 import com.google.code.facebookapi.schema.FriendsGetResponse;
7 import com.google.code.facebookapi.schema.User;
8 import com.google.code.facebookapi.schema.UsersGetInfoResponse;
9 import com.sun.syndication.feed.rss.Channel;
10 import com.sun.syndication.feed.rss.Item;
11 import com.sun.syndication.io.WireFeedInput;
12 import java.io.IOException;
14 import java.net.URISyntaxException;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.prefs.Preferences;
19 import org.xml.sax.InputSource;
22 private static final String APP_KEY = "ecdbd5dbacc168f9edfe470ccd7e401b";
23 private static final String APP_SEC = "22e3165366e958a6e049e1e0c1d30105";
26 private static String login() throws IOException, FacebookException, URISyntaxException {
27 FacebookJaxbRestClient login = new FacebookJaxbRestClient(APP_KEY, APP_SEC);
28 final String token = login.auth_createToken();
29 URI u = new URI("http://www.facebook.com/login.php?api_key=" + APP_KEY + "&auth_token=" + token);
30 System.out.println("authentication: " + u);
31 Runtime.getRuntime().exec("xdg-open " + u);
32 //Desktop.getDesktop().browse(u);
33 System.out.println("Visit the browser and press enter...");
36 return login.auth_getSession(token, true);
39 public static void main( String[] args) throws Exception {
40 WireFeedInput rssInput = new WireFeedInput();
41 URL rssUrl = new URL("http://blogs.apidesign.org/rss/");
42 Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));;
44 Preferences prefs = Preferences.userNodeForPackage(Main.class);
46 FacebookJaxbRestClient fb;
48 final String stored = prefs.get("session", null);
50 fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored);
53 final String session = login();
54 fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
55 prefs.put("session", session);
58 Long user = fb.users_getLoggedInUser();
60 if (fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
61 Preferences n = prefs.node("feeds");
62 for (Object o : rss.getItems()) {
64 String hex = Integer.toHexString(item.getLink().hashCode());
65 if (n.getLong(hex, -1L) != -1L) {
68 System.err.printf("link: %s date: %s title: %s\n", item.getLink(), item.getPubDate(), item.getTitle());
69 Long ok = fb.links_post(user, item.getLink(), item.getTitle());
70 System.out.println("posted as " + ok);
72 n.put(hex + ".url", item.getLink());
75 System.out.println("No permission to share links");