0

I'm using Facebook SDK in Android. I'm trying to create an app that uses facebook without having facebook SDK reside in some Activity or Fragment. i.e - I have a button that after I click it, I want to allow to share some image.

I got it to open the facebook SDK, ask for permission but it is stuck on Session.OPENING and then I can't request permissions -and thus can't publish.

My code is as follows (I didn't post the variables and helper function as they don't add info). It resided in a class and not inside an activity.

At this time I will call PostToFacebook once (it will try to open the session) and then I will call it again - so it will try to post. I will fix this duplicate later.

public void postToFacebook(byte[] ImageToPost, Activity activity, Bundle savedInstanceState) {

    Session session = Session.getActiveSession();

    if (session != null){
        SessionState state = session.getState();
        Log.d("facebook","in PostToFacebook. session: " + state.name());

        if (!state.isOpened()) {
            Log.d("facebook","Session is not open - aborting publish");
            return;
        }

        // Check for publish permissions    
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions)) {
            pendingPublishReauthorization = true;
            Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(activity, PERMISSIONS);
            session.requestNewPublishPermissions(newPermissionsRequest);
            return;
        }

        Bundle postParams = new Bundle();
        postParams.putString("name", "Name of post");
        postParams.putString("caption", "Caption of post [testing]");
        postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
        postParams.putString("link", "some link");
        postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

        Request.Callback callback= new Request.Callback() {
            public void onCompleted(Response response) {
                Log.d("facebook","Callback after onCompleted");

                JSONObject graphResponse = response
                                           .getGraphObject()
                                           .getInnerJSONObject();
                String postId = null;
                try {
                    postId = graphResponse.getString("id");
                } catch (JSONException e) {
                    Log.i(TAG,
                        "JSON error "+ e.getMessage());
                }
                FacebookRequestError error = response.getError();
                if (error != null) {
                    Toast.makeText(MainMirrorActivity.context
                         .getApplicationContext(),
                         error.getErrorMessage(),
                         Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainMirrorActivity.context
                             .getApplicationContext(), 
                             postId,
                             Toast.LENGTH_LONG).show();
                }
            }
        };

        Request request = new Request(session, "me/feed", postParams, 
                              HttpMethod.POST, callback);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    } else { 
        // open new session
        Log.d("facebook","opening a new session");
        if (savedInstanceState != null) {
            session = Session.restoreSession(activity, null, statusCallback, savedInstanceState);
        }
        if (session == null) {
            session = new Session(activity);
        }
        Session.setActiveSession(session);
        //SessionState state = session.getState();
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
            Log.d("facebook","openForRead");
            session.openForRead(new Session.OpenRequest(activity).setCallback(statusCallback));
        }

        if (!session.isOpened() && !session.isClosed()) {
            Log.d("facebook","openForRead if session not open and not closed");
            session.openForRead(new Session.OpenRequest(activity).setCallback(statusCallback));
        } else {
            Log.d("facebook","openActiveSession");
            Session.openActiveSession(activity, true, statusCallback);
        }
    }
}

When I'm checking the status it is always stuck on OPENING.

Many Thanks!

P.S - the code seems to almost work, so it might help someone anyway.

Roman
  • 4,443
  • 14
  • 56
  • 81

1 Answers1

0

I'm assuming that the code you have posted is the first point at which the session object is initialised. First I would try in your else block, when your getActiveSession == null

Session session = Session.openActiveSessionFromCache(getApplicationContext());

instead of

session = new Session(activity);

as your call to get a session. But I don't think that's your primary problem.

Attach a callback to the session so that you can perform actions once the state has completed opening.

After your null check, I would assume the possibility that the session is not yet ready, and attach a callback for when it is ready

    if(session.isOpened()){
            doSomething(session);
        } else {
            session.addCallback(new Session.StatusCallback() {
                @Override
                public void call(Session session, SessionState state, Exception exception) {
                    if(session.isOpened()){
                        doSomething(session);
                    }

                }
            });
        }
Pork 'n' Bunny
  • 6,740
  • 5
  • 25
  • 32
  • Thanks, tried that -sadly didn't help. I'm trying to port it to a Fragment now - but still with no success. – Roman May 29 '13 at 12:21