Home » Android » How to avoid already-authorized in Android Facebook SDK

How to avoid already-authorized in Android Facebook SDK

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m getting a completely useless page when I use the Single Sign on for Facebook’s Android SDK.

“You have already authorized happyapp.
Press “Okay” to continue.

This page would destroy user experience. How the heck do I get rid of it. Lots of people have been seeing this, but no solution is posted.

Even Facebook admits this is a problem, see: http://forum.developers.facebook.net/viewtopic.php?id=84548

Does anyone know any work-around?

How to&Answers:

The way I did it (without additional OAuth solution) was to store off the access token in preferences as Kieveli suggested. When the main activity starts, look up the token from preferences, if it’s not there initiate the authorization process and store the resulting token in preferences.

The harder part is to handle token expiration or de-authorization of your app (ie. the token is in preferences, but is no longer valid).

For that case, with every FB API/graph invocation, check for an exception indicating authentication failed. If it fails, initiate the authorization/token storing procedure again.

Answer:

using the suggestion here, this is the code that worked for me, hope it helps someone

before login do this

SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(FacebookLogin.this); 
                        String access_token = prefs.getString("access_token", null); 
                        Long expires = prefs.getLong("access_expires", -1);


                        if (access_token != null && expires != -1)
                        {
                            facebook.setAccessToken(access_token);
                            facebook.setAccessExpires(expires);
                        }


                        if (!facebook.isSessionValid())
                        {
                            facebook.authorize(FacebookLogin.this, new DialogListener() {
...

after you successfully logged in do this

String token = facebook.getAccessToken();
long token_expires = facebook.getAccessExpires();

SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(FacebookLogin.this);

prefs.edit().putLong("access_expires", token_expires).commit();

prefs.edit().putString("access_token", token).commit();

Answer:

I ran into this issue after any device had authorized my app with facebook. My emulator continued to work perfectly but I could not get the phone to get past the “okay” dialog screen and do anything meaningful. I added the facebook.FORCE_DIALOG_AUTH parameter to the authorize() call. Now it asks me to login once per device and sets the TOKEN and expiration in shared preferences as it should.

Button button_facebook = (Button) findViewById(R.id.button_share_on_facebook);
button_facebook.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        if (access_token != null) {
            facebook.setAccessToken(access_token);
        }

        if (expires != 0) {
            facebook.setAccessExpires(expires);
        }

        if (!facebook.isSessionValid()) {
            facebook.authorize(YourActivity.this, new String[] { "user_photos,publish_checkins,publish_actions,publish_stream" }, facebook.FORCE_DIALOG_AUTH, new DialogListener() {
                @Override
                public void onComplete(Bundle values) {
                    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token", facebook.getAccessToken());
                    editor.putLong("access_expires", facebook.getAccessExpires());
                    editor.commit();
                    postSomethingToFacebook();
                }

                @Override
                public void onFacebookError(FacebookError e) {
                    Log.e("Facebook-Authorize", "FacebookError Error: " + e.getMessage());
                }

                @Override
                public void onError(DialogError e) {
                    Log.e("Facebook-Authorize", "DialogError Error: "   + e.getMessage());
                }

                @Override
                public void onCancel() {
                    Log.w("Facebook-Authorize", "Cancelled.");
                }
            });
        } else {
            postSomethingToFacebook();
        }
});

Answer:

Bypass the SDK and use your own OAuth solution. Save the Access Token once acquired. Try to use it directly without sending the user to the facebook permission page. Once the token expires, you will need them to grant permission again. With advanced features comes lower level responsibilities (and more work).

Answer:

If you dont have FB app in your device means the popup will appeared in your safari browser.
Install the FB app in your device and try login using FB the already authorized screen not appeared.
Thank you.