Nizar B.
Nizar B.

Reputation: 3118

cannot open a facebook session with the SDK

I'm trying to post a message on my Facebook wall, here is a sample of my code:

 @Click (R.id.img_btn_facebook)
 @UiThread
 public void fbPostWall(){

    Session session = Session.getActiveSession();
    SessionState state = SessionState.OPENING;

    if (session == null)
      TestManager.getInstance().setActivity(getActivity()).setLoginListener(this).facebookLogin();

    Log.e("SESSION-STATUS", "" + session.getState());

    if (session != null || state.isOpened()){

        Log.e("FACEBOOK", "post link on the wall ...");
        Bundle params = new Bundle();
        params.putString("name", "This is a test");

        WebDialog feedDialog = (
                new WebDialog.FeedDialogBuilder(getActivity(),
                        session,
                        params))
                .setOnCompleteListener(new WebDialog.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(getActivity(),
                                        "Posted story, id: "+postId,
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                // User clicked the Cancel button
                                Toast.makeText(getActivity().getApplicationContext(),
                                        "Publish cancelled",
                                        Toast.LENGTH_SHORT).show();
                            }
                        } else if (error instanceof FacebookOperationCanceledException) {
                            // User clicked the "x" button
                            Toast.makeText(getActivity().getApplicationContext(),
                                    "Publish cancelled",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            // Generic, ex: network error
                            Toast.makeText(getActivity().getApplicationContext(),
                                    "Error posting story",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .build();
        feedDialog.show();
    }
}

When I click on the "img_btn_facebook" button a login session is opened if the user isn't already on Facebook. After that, the Log.e("SESSION-STATUS", "" + session.getState()); Always give SESSION-STATUS﹕ OPENING But the point is that I always get this error message from the Facebook SDK: com.facebook.FacebookException: Attempted to use a Session that was not open.

someone can help me please to fix it? I would like to post a message on my facebook wall thanks my Android app.

Upvotes: 0

Views: 2498

Answers (2)

Renato Probst
Renato Probst

Reputation: 6054

For someone having trouble with facebook sdk:

1 Download the samples, test them to see if it`s not your mobile (change the app id on sample to match yours);

2 Dont use a activity or fragment that has other purpose for facebook login, instead create one just for that, its probably a conflict.

3 Copy the facebook sample class you need (choose the activity or fragment) and call it through your code.

4 Adapt copied class to your needs.

Upvotes: 0

Kamlesh Meghwal
Kamlesh Meghwal

Reputation: 4972

Did you included onActivityResult ??

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode,
                resultCode, data);
        }

and in you manifest file

<activity
        android:name="com.facebook.LoginActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

and make sure that you have correct hash-key value in your facebook app

To generate hash-key,include following line of code in your main activity

try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "com.facebook.samples.hellofacebook", 
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

You will get hash key in console,include that key in facebook devleopr

Upvotes: 1

Related Questions