0

I'm having trouble while trying to retrieve an additional permission "publish_action".

I assume that user has already logged in via facebook thus basic permissions are granted. Then I have this code (from my fragment):

List<String> permissionsRequested = Arrays.asList("publish_actions");
List<String> permissionsActual = session.getPermissions();
if (!isSubsetOf(permissionsRequested,  permissionsActual)) {
    pendingPublishReauthorization = true;
    Session.NewPermissionsRequest newPermissionsRequest = new Session
        .NewPermissionsRequest(this, permissionsRequested);
    session.requestNewPublishPermissions(newPermissionsRequest);
    return;
}

Seeing from debug, after user grants permission, control goes to onActivityResult, where, according to facebook docs, I have

super.onActivityResult(requestCode, resultCode, data);
facebookUiHelper.onActivityResult(requestCode, resultCode, data);

uiHelper is also properly (acording to the docs) used in all those onResume&onDestroy methods.

In onCreate I have

super.onCreate(savedInstanceState);
facebookUiHelper = new UiLifecycleHelper(getActivity(), fbSessionCallback);
facebookUiHelper.onCreate(savedInstanceState);

where fbSessionCallback has overridden call() method, that calls my onFbSessionRestore():

if (pendingPublishReauthorization && 
    state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
    pendingPublishReauthorization = false;
    // do things with my new session, updated with granted permission. e.g. post to fb.
}

Trouble is that

  1. onActivityResult executes, but after that no callback ever called, so that overridden call() won't go, and onFbSessionCallback either.
  2. If I try to post again (start things from scratch) after that - it gets session without granted permission again, tries to request it and then goes facebook exception about pending newPublishRequest permission blah-blah

Can someone help to configure workflow from onActivityResult and later? I read facebook docs and I tend to blame they are written badly. Maybe it's just me too stupid... :(((

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Den Drobiazko
  • 1,077
  • 1
  • 13
  • 33
  • I think you have to check https://github.com/sromku/android-simple-facebook which can be easy way to integrate facebook sdk with your project. – Haresh Chhelana Jun 27 '14 at 08:59
  • Unfortunately, that is most likely not an option for this project. – Den Drobiazko Jun 27 '14 at 09:06
  • I mean - I'm not the only one working on this project, facebook API is already used throughout it and it would be strange to add a lib to impement this requestNewPermission workflow - don't want to create more mess in project structure, there's enough already ) – Den Drobiazko Jun 27 '14 at 09:54
  • Have a look at the Hello Facebook sample or the Scrumptious sample. Both are good examples on how to request follow up publish permissions after initial log in. – Ming Li Jun 27 '14 at 17:27
  • Looked through it. Adjusted my code to be as much alike as possible. Still not working: in onCreate session callback gets assigned to uiHelper, calback's call() method get's called, then onSessionStateChange get's called. After I request and grant permission - onResume get's called, onActivityResult - and that's all. I even tried to go into facebooks method's, but got myself lost and scared. Those fb guys leave comments and javadocs only for public methods ((( – Den Drobiazko Jul 01 '14 at 09:45

1 Answers1

0

This is how I have set it up:

Session session = Session.getActiveSession();
    if (session != null) {
        List<String> permissions = session.getPermissions();
        if (!permissions.contains("publish_actions")) {
            Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, "publish_actions");
            session.addCallback(callbacktwo);
            session.requestNewPublishPermissions(newPermissionsRequest);
            return;
        }
    }

This is in my onActivityResult but it doesn't need to be. I have used it in my onCreate before. I added a callback to the request which I can then use to do my next step.

Here is my callback:

private Session.StatusCallback callbacktwo = new Session.StatusCallback() {

    @Override
    public void call(Session session, SessionState state, Exception exception) {
        Intent in = new Intent(getActivity(), ClassHere.class);
        startActivity(in);
    }
};

Remember that the callback will respond twice, once when you start a permission request, and after it is completed. So you have to work around that/work that into your code.

RED_
  • 2,997
  • 4
  • 40
  • 59