Seraphis
Seraphis

Reputation: 1036

Android Facebook SDK - Failed to receive access token

I'm trying to use Facebook SDK in my Android app. Here's the snippet:

Facebook myFacebook = new Facebook("123456789012345");
myFacebook.authorize(LogInScreen.this, 
    new String[] {
        "publish_stream", 
        "email", 
        "user_about_me", 
        "user_birthday", 
        "user_website", 
        "friends_photos", 
        "user_photos"},
    Facebook.FORCE_DIALOG_AUTH,
    new DialogListener(){

        @Override
        public void onCancel() {
            Log.i("Facebook", "Facebook - cancel");
        }

        @Override
        public void onComplete(Bundle arg0) {
            Log.i("Facebook", "Facebook - complete, AccessToken: " + myFacebook.getAccessToken());
        }

        @Override
        public void onError(DialogError arg0) {
            Log.i("Facebook", "Facebook - error");
        }

        @Override
        public void onFacebookError(FacebookError error) {
            Log.i("Facebook", "Facebook - facebookError: " + error);
                    try {
                        myFacebook.logout(LogInScreen.this);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }}
    );
}
});

When I run this code, I can log in with my main facebook account, but when I try to use any other fb account I got error "Failed to receive access token". Am I missing something?

Upvotes: 4

Views: 8776

Answers (4)

s1m3n
s1m3n

Reputation: 623

In my case, the issue was that I had not added the correct Key Hash to the Facebook app configuration. I had only added the dbug Key Hash but not the "production" one and kept getting the "Failed to receive access token" error.

The Key Hash can be obtained like this:

keytool -exportcert -alias KEY_ALIAS -keystore CERT_STORE.jks | openssl sha1 -binary | openssl base64

Just make sure the alias, store and password are correct.

Upvotes: 0

Kornel
Kornel

Reputation: 91

I had the same problem, but sandbox wasn't solution. I had some restrictions about countries and my country wasn't there. I added it and it resolved my problem.

Upvotes: 2

Seraphis
Seraphis

Reputation: 1036

The problem was that the facebook app was set to sandbox mode, so only developer accounts could get access token from app's ID.

Upvotes: 13

user647826
user647826

Reputation:

If you put permission offline_access, the token expiry is 0.

For any one else having this issue (if you had put offline_access permission and later removed) follow the steps:

  • Go to your facebook profile setting and remove your app.
  • and then do login again.
  • Give permission to your app again.

Alternately you can do this:

  • Go to your facebook profile setting > Apps > Your_App and remove "Access my data any time" permission.
  • Save changes made.
  • Re-run Your_App(i.e. with no offline_access permission now).

Upvotes: 0

Related Questions