Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Facebook SDK authorize methods shows dialog every time

I'm writing an Android application that should upload photos to Facebook.

Everything works just fine except one thing. When I called the Facebook.authorize() method for the first time the Facebook dialog was shown with requested permissions and 'Allow'/'Don't Allow' buttons. That was OK. But when I run my app for the secong time I've got the same dialog, but with message that my application allowed already and suggestion to press OK button.

Is there a way to avoid this second dialog? Should I skip the authorize method in some conditions?

I tried to call the Facebook.isSessionValid() method before authorize method, but this did not help.

Here is my simplified code:

    mFacebook = new Facebook(APPLICATION_ID);
    mFacebookAsync = new AsyncFacebookRunner(mFacebook);

    if (mFacebook.isSessionValid()) {
        uploadPictureFile();
    }
    else {
        mFacebook.authorize(this, new String[] {"publish_stream"}, new Facebook.DialogListener() {

            @Override
            public void onFacebookError(FacebookError e) {
                Toast.makeText(PhotoFeederFacebookSendActivity.this, "Facebook error: " + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
                finishWithResultCode(RESULT_CANCELED);
            }

            @Override
            public void onError(DialogError e) {
                Toast.makeText(PhotoFeederFacebookSendActivity.this, "Facebook dialog error: " + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
                finishWithResultCode(RESULT_CANCELED);
            }

            @Override
            public void onComplete(Bundle values) {
                uploadPictureFile();
            }

            @Override
            public void onCancel() {Toast.makeText(PhotoFeederFacebookSendActivity.this, "Facebook authorization cancelled.", Toast.LENGTH_LONG).show();
                finishWithResultCode(RESULT_CANCELED);
            }
        });
    }

And here is my onActivityResult method:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mFacebook.authorizeCallback(requestCode, resultCode, data);
    }

Any help appreciated. Thanks.

like image 977
Alexey Erzyamkin Avatar asked Feb 17 '11 06:02

Alexey Erzyamkin


1 Answers

You need to call mFacebook.setAccessToken("your token") before mFacebook.isSessionValid() and you need to save the token to preference yourself

like image 163
magic Avatar answered Oct 05 '22 05:10

magic