src/main/java/cz/xelfi/feedbook/Main.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 09 Nov 2010 23:52:40 +0100
changeset 1 93c350696ff6
parent 0 258dc96b1636
child 2 21a08b9be14a
permissions -rw-r--r--
Using preferences to remember previous session login
jtulach@0
     1
package cz.xelfi.feedbook;
jtulach@0
     2
jtulach@1
     3
import com.google.code.facebookapi.FacebookException;
jtulach@0
     4
import com.google.code.facebookapi.FacebookJaxbRestClient;
jtulach@0
     5
import com.google.code.facebookapi.Permission;
jtulach@0
     6
import com.google.code.facebookapi.schema.FriendsGetResponse;
jtulach@0
     7
import com.google.code.facebookapi.schema.User;
jtulach@0
     8
import com.google.code.facebookapi.schema.UsersGetInfoResponse;
jtulach@1
     9
import com.sun.syndication.feed.rss.Channel;
jtulach@1
    10
import com.sun.syndication.feed.rss.Item;
jtulach@1
    11
import com.sun.syndication.io.WireFeedInput;
jtulach@1
    12
import java.io.IOException;
jtulach@0
    13
import java.net.URI;
jtulach@1
    14
import java.net.URISyntaxException;
jtulach@1
    15
import java.net.URL;
jtulach@0
    16
import java.util.Collections;
jtulach@0
    17
import java.util.List;
jtulach@1
    18
import java.util.prefs.Preferences;
jtulach@1
    19
import org.xml.sax.InputSource;
jtulach@0
    20
jtulach@0
    21
public class Main {
jtulach@0
    22
    private static final String APP_KEY = "ecdbd5dbacc168f9edfe470ccd7e401b";
jtulach@0
    23
    private static final String APP_SEC = "22e3165366e958a6e049e1e0c1d30105";
jtulach@0
    24
    
jtulach@1
    25
    
jtulach@1
    26
    private static String login() throws IOException, FacebookException, URISyntaxException {
jtulach@0
    27
        FacebookJaxbRestClient login = new FacebookJaxbRestClient(APP_KEY, APP_SEC);
jtulach@0
    28
        final String token = login.auth_createToken();
jtulach@0
    29
        URI u = new URI("http://www.facebook.com/login.php?api_key=" + APP_KEY + "&auth_token=" + token);
jtulach@1
    30
        System.out.println("authentication: " + u);
jtulach@0
    31
        Runtime.getRuntime().exec("xdg-open " + u);
jtulach@0
    32
        //Desktop.getDesktop().browse(u);
jtulach@0
    33
        System.out.println("Visit the browser and press enter...");
jtulach@0
    34
        System.in.read();
jtulach@0
    35
        
jtulach@1
    36
        return login.auth_getSession(token, true); 
jtulach@1
    37
    }
jtulach@1
    38
    
jtulach@1
    39
    public static void main( String[] args) throws Exception {
jtulach@1
    40
        WireFeedInput rssInput = new WireFeedInput();
jtulach@1
    41
        URL rssUrl = new URL("http://blogs.apidesign.org/rss/");
jtulach@1
    42
        Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));;
jtulach@1
    43
        System.err.printf("stream from %s\n", rss.getUri());
jtulach@1
    44
        for (Object o : rss.getItems()) {
jtulach@1
    45
            Item item = (Item)o;
jtulach@1
    46
            
jtulach@1
    47
            System.err.printf("link: %s date: %s title: %s\n", item.getLink(), item.getPubDate(), item.getTitle());
jtulach@1
    48
        }
jtulach@0
    49
        
jtulach@1
    50
        Preferences prefs = Preferences.userNodeForPackage(Main.class);
jtulach@1
    51
        
jtulach@1
    52
        FacebookJaxbRestClient fb;
jtulach@1
    53
        for (;;) {
jtulach@1
    54
            final String stored = prefs.get("session", null);
jtulach@1
    55
            if (stored != null) {
jtulach@1
    56
                fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored);
jtulach@1
    57
                break;
jtulach@1
    58
            }
jtulach@1
    59
            final String session = login();
jtulach@1
    60
            fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
jtulach@1
    61
            prefs.put("session", session);
jtulach@1
    62
            break;
jtulach@1
    63
        }
jtulach@0
    64
        Long user = fb.users_getLoggedInUser();
jtulach@0
    65
        final FriendsGetResponse ret = fb.friends_get(user);
jtulach@0
    66
        final UsersGetInfoResponse info = fb.users_getInfo(ret.getUid(), Collections.<CharSequence>singleton("last_name"));
jtulach@0
    67
        final List<User> arr = info.getUser();
jtulach@0
    68
        System.out.println("friends: " + arr.size());
jtulach@0
    69
        for (int i = 0; i < arr.size(); i++) {
jtulach@0
    70
            System.out.println("  " + i + ". = " + arr.get(i).getLastName());
jtulach@0
    71
        }
jtulach@0
    72
        
jtulach@0
    73
        String url = "http://apidesign.org";
jtulach@0
    74
        String cmd = "Testing new application";
jtulach@0
    75
jtulach@0
    76
        if (fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
jtulach@1
    77
            Long ok = 1L; //fb.links_post(user, url, cmd);
jtulach@0
    78
            System.out.println("posted as " + ok);
jtulach@0
    79
        } else {
jtulach@0
    80
            System.out.println("No permission to share links");
jtulach@0
    81
        }
jtulach@0
    82
    }
jtulach@0
    83
}