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.sun.syndication.feed.rss.Channel;
7 import com.sun.syndication.feed.rss.Item;
8 import com.sun.syndication.io.WireFeedInput;
9 import java.awt.Desktop;
10 import java.io.IOException;
12 import java.net.URISyntaxException;
14 import java.util.prefs.Preferences;
15 import org.xml.sax.InputSource;
18 private static final String APP_KEY = "ecdbd5dbacc168f9edfe470ccd7e401b";
19 private static final String APP_SEC = "22e3165366e958a6e049e1e0c1d30105";
22 private static String login() throws IOException, FacebookException, URISyntaxException {
23 FacebookJaxbRestClient login = new FacebookJaxbRestClient(APP_KEY, APP_SEC);
24 final String token = login.auth_createToken();
25 URI u = new URI("http://www.facebook.com/login.php?api_key=" + APP_KEY + "&auth_token=" + token);
26 System.out.println("authentication: " + u);
28 Desktop.getDesktop().browse(u);
29 } catch (Exception ex) {
30 Runtime.getRuntime().exec("xdg-open " + u);
32 System.out.println("Visit the browser and press enter...");
35 return login.auth_getSession(token, false);
38 public static void main( String[] args) throws Exception {
39 WireFeedInput rssInput = new WireFeedInput();
40 URL rssUrl = new URL("http://blogs.apidesign.org/rss/");
41 Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));;
43 Preferences prefs = Preferences.userNodeForPackage(Main.class);
45 FacebookJaxbRestClient fb;
48 final String stored = prefs.get("session", null);
51 fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored);
52 user = fb.users_getLoggedInUser();
54 } catch (FacebookException ex) {
55 System.err.println(ex.getMessage());
58 final String session = login();
59 fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
60 user = fb.users_getLoggedInUser();
61 prefs.put("session", session);
65 if (fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
66 Preferences n = prefs.node("feeds");
67 for (Object o : rss.getItems()) {
69 String hex = Integer.toHexString(item.getLink().hashCode());
70 if (n.getLong(hex, -1L) != -1L) {
73 System.err.printf("link: %s date: %s title: %s\n", item.getLink(), item.getPubDate(), item.getTitle());
74 Long ok = fb.links_post(user, item.getLink(), item.getTitle());
75 System.out.println("posted as " + ok);
77 n.put(hex + ".url", item.getLink());
80 System.out.println("No permission to share links");