Facebook SDK: not "opened" session state while creating WebDialog.FeedDialogBuilder

1.7k Views Asked by At

I faced to such a wierd bug while implementing Facebook share info. I made all stages as they are described on Facebook tutorial Facebook tutorial - share info

but while I was trying to create example of WebDialog.feedDialog

    private void publishFeedDialog() {
        Bundle params = new Bundle();
        params.putString("name", getString(R.string.name_fb));
        params.putString("description", getString(R.string.description_fb));
        params.putString("link", getString(R.string.share_link_fb));
        params.putString("picture", getString(R.string.pictute_url_fb));
        
        Session session = Session.getActiveSession();
        
        Log.i(TAG, "session = " + session + " isOpen = " + session.isOpened() + " isClosed = " + session.isClosed());
        
        WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(this, Session.getActiveSession(), params))
          .setOnCompleteListener(new OnCompleteListener() {

                @Override
                public void onComplete(Bundle values, FacebookException error) {
                    if (error == null) {
                     
                        // When the story is posted, echo the success and the post Id.
                        final String postId = values.getString("post_id");
                        
                        if (postId != null) {
                            Toast.makeText(MainActivity.this, "Posted story, id: " + postId,
                                Toast.LENGTH_SHORT).show();
                        } else {
                            // User clicked the Cancel button
                            Toast.makeText(MainActivity.this, "Publish cancelled", 
                                Toast.LENGTH_SHORT).show();
                        }
                        
                    } else if (error instanceof FacebookOperationCanceledException) {
                        // User clicked the "x" button
                        Toast.makeText(MainActivity.this, "Publish cancelled", Toast.LENGTH_SHORT).show();
                        
                    } else {
                        // Generic, ex: network error
                        Toast.makeText(MainActivity.this, "Error posting story", Toast.LENGTH_SHORT).show();
                    }
                }

            }).build();
        
        feedDialog.show();
    }

I caught FacebookException!

09-22 19:34:49.325: E/ActivityThread(12202): Failed to find provider info for com.facebook.katana.provider.PlatformProvider
09-22 19:34:49.325: I/MainActivity(12202): session = {Session state:CREATED, token:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}, appId:1111111111111} isOpen = false isClosed = false
09-22 19:34:49.325: D/AndroidRuntime(12202): Shutting down VM
09-22 19:34:49.325: W/dalvikvm(12202): threadid=1: thread exiting with uncaught exception (group=0x418cc700)
09-22 19:34:49.325: E/ActivityThread(12202): Failed to find provider info for com.facebook.wakizashi.provider.PlatformProvider
09-22 19:34:49.405: D/dalvikvm(12202): GC_FOR_ALLOC freed 891K, 9% free 10493K/11452K, paused 21ms, total 21ms
09-22 19:34:52.365: E/AndroidRuntime(12202): FATAL EXCEPTION: main
09-22 19:34:52.365: E/AndroidRuntime(12202): java.lang.IllegalStateException: Could not execute method of the activity
09-22 19:34:52.365: E/AndroidRuntime(12202):  at android.view.View$1.onClick(View.java:3633)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at android.view.View.performClick(View.java:4240)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at android.view.View$PerformClick.run(View.java:17721)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at android.os.Handler.handleCallback(Handler.java:730)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at android.os.Looper.loop(Looper.java:137)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at android.app.ActivityThread.main(ActivityThread.java:5103)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at java.lang.reflect.Method.invokeNative(Native Method)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at java.lang.reflect.Method.invoke(Method.java:525)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at dalvik.system.NativeStart.main(Native Method)
09-22 19:34:52.365: E/AndroidRuntime(12202): Caused by: java.lang.reflect.InvocationTargetException
09-22 19:34:52.365: E/AndroidRuntime(12202):  at java.lang.reflect.Method.invokeNative(Native Method)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at java.lang.reflect.Method.invoke(Method.java:525)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at android.view.View$1.onClick(View.java:3628)
09-22 19:34:52.365: E/AndroidRuntime(12202):  ... 11 more
09-22 19:34:52.365: E/AndroidRuntime(12202): Caused by: com.facebook.FacebookException: Attempted to use a Session that was not open.
09-22 19:34:52.365: E/AndroidRuntime(12202):  at com.facebook.widget.WebDialog$BuilderBase.<init>(WebDialog.java:485)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at com.facebook.widget.WebDialog$FeedDialogBuilder.<init>(WebDialog.java:669)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at com.kaeriasarl.psslite.activities.MainActivity.publishFeedDialog(MainActivity.java:131)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at com.kaeriasarl.psslite.activities.MainActivity.shareLinkToFacebook(MainActivity.java:116)
09-22 19:34:52.365: E/AndroidRuntime(12202):  at com.kaeriasarl.psslite.activities.MainActivity.onClickFeature(MainActivity.java:99)
09-22 19:34:52.365: E/AndroidRuntime(12202):  ... 14 more
09-22 19:34:52.475: I/GAV3(12202): Thread[GAThread,5,main]: No campaign data found.

As I can understand from logs the main trouble is that result of Session.getActiveSession() should not be null and should be opened but as I see session has status only CREATED! But I made all the stages as Facebook tutorial said! Help, please!

2

There are 2 best solutions below

2
On BEST ANSWER

After investigation I solved my problem:

1) Before creating feedDialog I should check if Active Session is opened and if not then I should directly open Active Session

 if (Session.getActiveSession() == null || !Session.getActiveSession().isOpened()) {
        Session.openActiveSession(MainActivity.this, true, callback);
    } else {
        publishFeedDialog();
    }

2) and call publishFeedDialog() in Session.StatusCallback

    private Session.StatusCallback callback = new Session.StatusCallback() {
  
  @Override
  public void call(Session session, SessionState state, Exception exception) {
   
   if (state.isOpened() && isFbShare) {
    publishFeedDialog();
   }
  }
 };

0
On

In addition to the accepted answer above, if this is a first time user experience flow, the user will need to login to create a session. In order to do this, you'll need to add LoginActivity to your AndroidManifest.xml -

    <activity
        android:name="com.facebook.LoginActivity"
        android:label="@string/title_activity_facebook_login" >
    </activity>