skip to Main Content

I want to ask the user of my android app the permission to publish on facebook when a custom share button (it’s just an ImageView) is pressed. On button’s OnClick method I execute this block:

CallbackManager facebookCallbackManager;    

...

facebookCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(facebookCallbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                shareContent(activity, content, true);
            }
            @Override
            public void onCancel() { }

            @Override
            public void onError(FacebookException error) { }
        });
LoginManager.getInstance().logInWithPublishPermissions(activity, Collections.singletonList("publish_actions"));

And then I override:

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

The problem is that the request never comes: an everlasting spinner wheel is presented and no callback (nor success, nor cancel, nor error) is called.

a. The user is already logged in, according to:

public static boolean isLoggedIn() {
   AccessToken accessToken = AccessToken.getCurrentAccessToken();
   return accessToken != null;
}

b. FacebookSdk.isInitialized() is true

c. Publish permissions are not granted, according to:

private static boolean hasPublishPermissions() {
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    return accessToken != null && accessToken.getPermissions().contains("publish_actions");
}

d. Other uses of FB SDK are used through the app and they are functioning.

e. I am an app admin on FB dashboard

Any idea on the problem?

Important PS:

Since Facebook’s API is extremely stable, depending on the time of the day or the position of the stars, without changing code I have three possible outcomes:

  1. As described before, an everlasting spinner wheel.

  2. The callback fires the onCancel method without the user interaction.

  3. It shares the content without asking for confirmation – this gave me a nice unwanted video posted on my personal FB, without me noticing 🙂 –

    PS2:
    Even the classic LoginManager.getInstance().logInWithReadPermissionnow is having the same issue. It never had it before.

2

Answers


  1. I think there is mistake for callback manager.
    you declare and assign CallbackManager facebookCallbackManager;
    facebookCallbackManager = CallbackManager.Factory.create();

    Here you can see there is cbManager instead of facebookCallbackManager
    so please double check for that.

                                                    V
    LoginManager.getInstance().registerCallback(cbManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                shareContent(activity, content, true);
            }
            @Override
            public void onCancel() { }
    
            @Override
            public void onError(FacebookException error) { }
        });
    
    Login or Signup to reply.
  2. then you can share content without everlasting spinner

     LoginManager.getInstance().registerCallback(mCallbackManagerFacebook, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                System.out.println("Success");
                if (loginResult.getRecentlyGrantedPermissions().contains("publish_actions")) {
                    new shareContent(activity, content, true);
    
                }  else {
                    //Toast.makeText(youractivity.this, "Successfully Connected to Facebook.", Toast.LENGTH_LONG).show();
    
                    GetFacebookPublishPermissions();
    
                }
            }
    
            @Override
            public void onCancel() {
                //Log.d(TAG_CANCEL, "On cancel");
                LoginManager.getInstance().logOut();
            }
    
            @Override
            public void onError(FacebookException error) {
                Toast.makeText(youractivity.this, "Looks like we are unable to connect to Facebook Servers.", Toast.LENGTH_LONG).show();
                LoginManager.getInstance().logOut();
                // Log.d(TAG_ERROR, error.toString());
            }
        });
    

    and getFacebookSharepermission

     public void GetFacebookPublishPermissions(){
        if(LoginManager.getInstance()!=null)
        {
            LoginManager.getInstance().logInWithPublishPermissions(youractivity.this, Arrays.asList("publish_actions"));
    
        }
    }
    

    Update

    you can call this function like below

          AccessToken token = com.facebook.AccessToken.getCurrentAccessToken();
            if(token!=null)
            {
                if (token.getPermissions().contains("publish_actions")) {
                new shareContent(activity, content, true);
            }else{
            GetFacebookPublishPermissions();
            }
       }else{
             LoginManager.getInstance().logInWithReadPermissions(this, 
             Arrays.asList("public_profile", "email", "user_birthday"));
       }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search