vendredi 27 février 2015

Combine Facebook login and permission request into one

I am trying to create a dialog that asks users to login with Facebook. In reality I don’t care much for the login per se: all I really want is permission to post photos on behalf of the user; nothing else. Below is my Dialog in its entirety. Will someone please correct the code for me? Right now its crashing with some NPE within the Facebook library. I compare my code with the sample HelloFacebookSampleActivity. I can see that the two are obviously different. But I can’t seem to fix mine correctly. Again, I am hoping to at once ask the user to Login and get the permission as opposed to doing it in two steps.



public class FacebookOptInFragment extends DialogFragment {
private final String TAG = FacebookOptInFragment.class.getSimpleName();
private LoginButton loginButton;
private GraphUser user;
private static final String PERMISSION = "publish_actions";

private UiLifecycleHelper uiHelper;

private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if ((exception instanceof FacebookOperationCanceledException || exception instanceof FacebookAuthorizationException)) {
new AlertDialog.Builder(FacebookOptInFragment.this.getActivity())
.setTitle("Cancel")
.setMessage("Permission not granted")
.setPositiveButton("Ok", null)
.show();
}
updateUI();
}

private void updateUI() {
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
//user has agreed to post photos… so make a note of that
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.facebook_optin, container);
loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
FacebookOptInFragment.this.user = user;
updateUI();
}
});

return view;
}

}








Looking at the HelloFacebookSampleActivity, I see that the Login process is separate from the permission request process. Is there a way to combine the two? That is, when I ask a user to Login with Facebook, I “automatically” get the permissions I need. What I mean specifically is that I don’t want two different windows at two different times. This is important for my case because all I really want is the permission to share photos for the user; the login itself is irrelevant. So how might I combine the two?


The sample codes from HelloFacebookSampleActivity mentioned are


For Login



loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
HelloFacebookSampleActivity.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();
}
});


For permission request



session.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, PERMISSION));


Will someone please show a complete code snippet showing how to ask for permission while login in. I am very new to this so filling in the blanks is difficult, so a complete snippet would be helpful, which is why I submit my entire class for editing.


Aucun commentaire:

Enregistrer un commentaire