Automate login request
authorJaroslav Tulach <jtulach@netbeans.org>
Wed, 10 Nov 2010 17:46:35 +0100
changeset 5e1d564765efd
parent 4 c366470a05e1
child 6 cb3f8481a580
Automate login request
src/main/java/cz/xelfi/feedbook/Main.java
     1.1 --- a/src/main/java/cz/xelfi/feedbook/Main.java	Wed Nov 10 17:26:00 2010 +0100
     1.2 +++ b/src/main/java/cz/xelfi/feedbook/Main.java	Wed Nov 10 17:46:35 2010 +0100
     1.3 @@ -23,7 +23,13 @@
     1.4          FacebookJaxbRestClient login = new FacebookJaxbRestClient(APP_KEY, APP_SEC);
     1.5          final String token = login.auth_createToken();
     1.6          URI u = new URI("http://www.facebook.com/login.php?api_key=" + APP_KEY + "&auth_token=" + token);
     1.7 -        System.out.println("authentication: " + u);
     1.8 +        String msg = "authentication";
     1.9 +        browser(msg, u);
    1.10 +        return login.auth_getSession(token, false); 
    1.11 +    }
    1.12 +
    1.13 +    private static void browser(String msg, URI u) throws IOException {
    1.14 +        System.out.println(msg + ": " + u);
    1.15          try {
    1.16              Desktop.getDesktop().browse(u);
    1.17          } catch (Exception ex) {
    1.18 @@ -31,8 +37,6 @@
    1.19          }
    1.20          System.out.println("Visit the browser and press enter...");
    1.21          System.in.read();
    1.22 -        
    1.23 -        return login.auth_getSession(token, false); 
    1.24      }
    1.25      
    1.26      public static void main( String[] args) throws Exception {
    1.27 @@ -62,22 +66,25 @@
    1.28              break;
    1.29          }
    1.30          
    1.31 -        if (fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
    1.32 -            Preferences n = prefs.node("feeds");
    1.33 -            for (Object o : rss.getItems()) {
    1.34 -                Item item = (Item) o;
    1.35 -                String hex = Integer.toHexString(item.getLink().hashCode());
    1.36 -                if (n.getLong(hex, -1L) != -1L) {
    1.37 -                    continue;
    1.38 -                }
    1.39 -                System.err.printf("link: %s date: %s title: %s\n", item.getLink(), item.getPubDate(), item.getTitle());
    1.40 -                Long ok = fb.links_post(user, item.getLink(), item.getTitle());
    1.41 -                System.out.println("posted as " + ok);
    1.42 -                n.putLong(hex, ok);
    1.43 -                n.put(hex + ".url", item.getLink());
    1.44 +        while (!fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
    1.45 +            URI u = new URI("https://graph.facebook.com/oauth/authorize?"
    1.46 +                    + "client_id=" + APP_KEY
    1.47 +                    + "&redirect_uri=http://www.facebook.com/connect/login_success.html"
    1.48 +                    + "&scope=publish_stream");
    1.49 +            browser("Need permission to publish links", u);
    1.50 +        }
    1.51 +        Preferences n = prefs.node("feeds");
    1.52 +        for (Object o : rss.getItems()) {
    1.53 +            Item item = (Item) o;
    1.54 +            String hex = Integer.toHexString(item.getLink().hashCode());
    1.55 +            if (n.getLong(hex, -1L) != -1L) {
    1.56 +                continue;
    1.57              }
    1.58 -        } else {
    1.59 -            System.out.println("No permission to share links");
    1.60 +            System.err.printf("link: %s date: %s title: %s\n", item.getLink(), item.getPubDate(), item.getTitle());
    1.61 +            Long ok = fb.links_post(user, item.getLink(), item.getTitle());
    1.62 +            System.out.println("posted as " + ok);
    1.63 +            n.putLong(hex, ok);
    1.64 +            n.put(hex + ".url", item.getLink());
    1.65          }
    1.66      }
    1.67  }