yohjizzz's Blog

I'm a Programmer.

FacebookClient にプロキシを設定

RestFb でプロキシを指定するにはチョイと面倒で、
DefaultFacebookClient に指定する DefaultWebRequestor の openConnection() を拡張する必要があるようです。

    public static final void main(String[] args) throws Exception {
        
        FacebookClient facebookClient = new DefaultFacebookClient(
            "Facebook あくせすとーくん",
            new DefaultWebRequestor() {
                @Override
                protected HttpURLConnection openConnection(URL url) throws IOException {
                    SocketAddress address = new InetSocketAddress("0.0.0.0", 0); // ←プロキシのホストとポート指定..
                    return (HttpURLConnection)url.openConnection(new Proxy(Type.HTTP, address));
                }
            },
            new DefaultJsonMapper());
        
        User user = facebookClient.fetchObject("me", User.class);
        System.out.println("User.email = [" + user.getEmail() + "].");
        System.out.println("User.bio = [" + user.getBio() + "].");
        System.out.println("User.link = [" + user.getLink() + "].");
        System.out.println("User.birthday = [" + user.getBirthday() + "].");
    }

これで OK !

RestFB is a simple and flexible Facebook Graph API and Old REST API client written in Java.
It is open source software released under the terms of the MIT License.

RestFB is a simple but powerful Facebook Graph API and REST API client written in Java