0

I have coded an App were I need Facebook sharing (post to wall).

The app seams to work: I get a dialog were I am asked if I want to share on Facebook. After it is supposed to authenticate, and if not, it should give me an error message, which it doesn't.
It just gives me the same dialog.

Can somebody have a look at my code please?

 public class ShareOnFacebook extends Activity{

    private static final String APP_ID = "138652356232418";
    private static final String[] PERMISSIONS = new String[] {"publish_stream"};

    private static final String TOKEN = "access_token";
        private static final String EXPIRES = "expires_in";
        private static final String KEY = "facebook-credentials";

        private Facebook facebook;
    private String messageToPost;

    public boolean saveCredentials(Facebook facebook) {
        Editor editor = 
        getApplicationContext().getSharedPreferences(KEY,Context.MODE_PRIVATE).edit();    
        editor.putString(TOKEN, facebook.getAccessToken());
        editor.putLong(EXPIRES, facebook.getAccessExpires());
        return editor.commit();
    }

    public boolean restoreCredentials(Facebook facebook) {
        SharedPreferences sharedPreferences =    
        getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
        facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
        facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
        return facebook.isSessionValid();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        facebook = new Facebook(APP_ID);
        restoreCredentials(facebook);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.facebook_dialog);

        String facebookMessage = getIntent().getStringExtra("facebookMessage");
        if (facebookMessage == null){
            facebookMessage = "Wir sind auf dem Hamburger Weinachtsmarkt, kommt 
                doch einen Glühwein mit uns trinken!";
        }

        messageToPost = facebookMessage;  
        }

        public void doNotShare(View button){
        finish();
        }

        public void share(View button){
            if (! facebook.isSessionValid()) {
            loginAndPostToWall();
            }

            else {
                postToWall(messageToPost);
            }
        }

        public void loginAndPostToWall(){
             facebook.authorize(this,  PERMISSIONS, new LoginDialogListener());
        }

        public void postToWall(String message){
            Bundle parameters = new Bundle();
                parameters.putString("message", message);
                facebook.dialog(this, "stream.publish", parameters, new     
                    WallPostDialogListener());
        }

        class LoginDialogListener implements DialogListener {
             public void onComplete(Bundle values) {
                saveCredentials(facebook);
                if (messageToPost != null){
                postToWall(messageToPost);
        }
        }

        public void onFacebookError(FacebookError error) {
            showToast("Authentication with Facebook failed!");
            finish();
        }

        public void onError(DialogError error) {
            showToast("Authentication with Facebook failed!");
            finish();
        }

        public void onCancel() {
            showToast("Authentication with Facebook cancelled!");
            finish();
        }
    }

        class WallPostDialogListener implements DialogListener {
        public void onComplete(Bundle values) {
                    final String postId = values.getString("post_id");
                    if (postId != null) {
                    showToast("Message posted to your facebook wall!");
                } else {
                    showToast("Wall post cancelled!");
                }
                finish();
            }

        public void onFacebookError(FacebookError e) {
            showToast("Failed to post to wall!");
            e.printStackTrace();
            finish();
        }

        public void onError(DialogError e) {
            showToast("Failed to post to wall!");
            e.printStackTrace();
            finish();
        }

        public void onCancel() {
            showToast("Wall post cancelled!");
            finish();
        }
            }
        private void showToast(String message){
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
        }
      }

There is a button on my main menu which has an intent to the class I've pasted. On my Facebook share button I have an intent:

Intent postOnFacebookWallIntent = new Intent(this, ShareOnFacebook.class);
            postOnFacebookWallIntent.putExtra("facebookMessage", "is integrating stuff again.");
            startActivity(postOnFacebookWallIntent);

OK, I'll make it more clear.

This class is the class which takes care of all my Facebook API stuff. When I integrate it with my app or any app, it gives the user a dialog asking if you want to share with Facebook. If you press yes, it should authenticate with Facebook and post the message or if it fails, it should give me an error message, which it doesn't. It try to authenticate and then gives me the same dialog, which it shouldn't.

Now my question is what am I doing wrong, why is it not doing what it should.

Adinia
  • 3,722
  • 5
  • 40
  • 58
Marc Bluemner
  • 11
  • 1
  • 5
  • 1
    There is no code in your question now; please edit the question and add it too. – Adinia Oct 16 '11 at 16:52
  • Really unclear what you mean when you say `On my Facebook share button ive got an intent:`. – Edwin Oct 16 '11 at 16:56
  • Did you register the app with Facebook and use the correct key? – Yevgeny Simkin Oct 16 '11 at 18:30
  • Hey can someone tell me why am getting crash at this line startActivity(postOnFacebookWallIntent); I have followed the above mentioned procedure.. one more thing whats the thing in this xml file "facebook_dialog"??? – Quick learner Feb 02 '12 at 09:37

1 Answers1

0

When you register your app with Facebook they need a key to associate to your app. It's a tad confusing because the key you get when you run your app in eclipse is not the same key you'll be using to publish it and the key generation will LOOK like it's giving you the correct result even if you put in the wrong password, so you have to be super duper sure that A) you're generating the right key (debug or real) and B) you're using the right password AND alias.

My guess is that you have a key mismatch and are simply failing authentication with FB.

This post covers it in excellent detail.

Community
  • 1
  • 1
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236