Wow, FB SDK su*** big time. I've been spending way too much time trying to figure out why a simple login wont work. The "HelloFacebookSample" works fine and logs into FB using my account but when I try to implement it in my app I get nothing!
I actually copy-pasted almost the entire Activity they wrote just to see if it will work, why did they do it so cumbersome?
When I print out the "session" this is what I get:
08-03 17:49:02.074: D/SESSION(30213): {Session state:OPENING, token:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}, appId:
Basically, I have the "log in with Facebook button" set up and when I click on it it tries to connect but with no success.
Here is the activity:
public class FacebookLogin extends Activity {
private Button postStatusUpdateButton;
private Button postPhotoButton;
private Button pickFriendsButton;
private Button pickPlaceButton;
private LoginButton loginButton;
private ProfilePictureView profilePictureView;
private TextView greeting;
private ViewGroup controlsContainer;
private GraphUser user;
private GraphPlace place;
private List<GraphUser> tags;
private boolean canPresentShareDialog;
private boolean canPresentShareDialogWithPhotos;
private PendingAction pendingAction = PendingAction.NONE;
private enum PendingAction {
NONE,
POST_PHOTO,
POST_STATUS_UPDATE
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private FacebookDialog.Callback dialogCallback = new FacebookDialog.Callback() {
@Override
public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
Log.d("HelloFacebook", String.format("Error: %s", error.toString()));
}
@Override
public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
Log.d("HelloFacebook", "Success!");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.fb_login);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
FacebookLogin.this.user = user;
updateUI();
// It's possible that we were waiting for this.user to be populated in order to post a
// status update.
// handlePendingAction();
}
});
profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture);
greeting = (TextView) findViewById(R.id.greeting);
}
private void updateUI() {
Session session = Session.getActiveSession();
if(session==null){
session = new Session.Builder(getApplicationContext()).setApplicationId(getString(R.string.app_id)).build();
}
session = Session.getActiveSession();
Log.d("SESSION", session+"");
// Log.d("SESSION", session.toString()+"");
boolean enableButtons = (session != null && session.isOpened());
Log.d("SESSION_BOOL", enableButtons+"");
if (enableButtons && user != null) {
profilePictureView.setProfileId(user.getId());
greeting.setText(getString(R.string.hello_user, user.getFirstName()));
} else {
profilePictureView.setProfileId(null);
greeting.setText(null);
}
}
private interface GraphObjectWithId extends GraphObject {
String getId();
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (pendingAction != PendingAction.NONE &&
(exception instanceof FacebookOperationCanceledException ||
exception instanceof FacebookAuthorizationException)) {
new AlertDialog.Builder(FacebookLogin.this)
.setTitle("Cancel")
.setMessage("Permissions not granted")
.setPositiveButton(R.string.ok, null)
.show();
pendingAction = PendingAction.NONE;
} else if (state == SessionState.OPENED_TOKEN_UPDATED) {
handlePendingAction();
}
updateUI();
}
@SuppressWarnings("incomplete-switch")
private void handlePendingAction() {
PendingAction previouslyPendingAction = pendingAction;
// These actions may re-set pendingAction if they are still pending, but we assume they
// will succeed.
pendingAction = PendingAction.NONE;
}
}
Added into Manifest:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
Please help me understand what am I missing?
You need to add in the manifest
also you need to add the list of permission when you press the login button