Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: traced the NPE I got from the demo to the Login.class constructor being called even on the redirect to facebook for login case.

...

Code Block
import com.facebook.api.FacebookException;
import com.facebook.api.FacebookRestClient;
import com.facebook.api.schema.FriendsGetResponse;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;

import java.io.IOException;
import java.util.List;

public class Login extends WebPage {

    public Login() {
        try {
            FacebookRestClient client = ((FaceBookSession) getSession()).getClient();

            client.friends_get();

            FriendsGetResponse fbResponse = (FriendsGetResponse) client.getResponsePOJO();
            List<Long> friends = fbResponse.getUid();

            add(new ListView("friends", friends) {

                protected void populateItem(ListItem listItem) {
                    listItem.add(new Label("friend", listItem.getModelObjectAsString()));
                }
            });

        } catch (FacebookException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Unregistered Application to Facebook User Problem

If your facebook profile is not associated with the application then client.friends_get() in the Login page will throw a NullPointerException.
This occurs because although the onUnauthorizedInstantiation method defined in the application class forces the login to occur in this case for some reason the Login page constructor is still called before the redirect takes place.

An ugly hack to make the demo work in this case is to add a test before client.friends_get() like:

Code Block

    if (client == null) {
        WebMarkupContainer friends = new WebMarkupContainer("friends");
	friends.add(new Label("friend", "missing frields"));
    } else {
         client.friends_get();

         FriendsGetResponse fbResponse = (FriendsGetResponse) client.getResponsePOJO();
         List<Long> friends = fbResponse.getUid();

         add(new ListView("friends", friends) {

             protected void populateItem(ListItem listItem) {
                  listItem.add(new Label("friend", listItem.getModelObjectAsString()));
             }
         });

    }