Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: post to facebook programatically with FB SDK 4.x

I want to post to facebook without dialog, just programatically, i am trying with

public void login(CallbackManager callbackManager) {
    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d(LOGTAG, "facebook login success");
            share("kk");
        }

        @Override
        public void onCancel() {
            Log.w(LOGTAG, "facebook login canceled");
            share("kk");
        }

        @Override
        public void onError(FacebookException exception) {
            Log.e(LOGTAG, "facebook login error");
            exception.printStackTrace();
        }
    });

    LoginManager.getInstance().logInWithPublishPermissions((Activity) context, Arrays.asList("publish_actions"));
}

public void share(final String msg) {

    Log.d(LOGTAG, "facebook sharing new message");
    Set<String> permissions = AccessToken.getCurrentAccessToken().getPermissions();
    AccessToken accessToken = AccessToken.getCurrentAccessToken();

    Bundle postParams = new Bundle();
    postParams.putString("name", "yourTitle");
    postParams.putString("caption", "yourCaption");
    postParams.putString("description", "yourDesc");
    postParams.putString("message", "yourMessage");

    GraphRequest request = new GraphRequest(accessToken, "me/feed", postParams, HttpMethod.POST, null);
    AsynTaskGraphRequest asynTaskGraphRequest = new AsynTaskGraphRequest(request);
    asynTaskGraphRequest.execute();
}

the problem is that when app logs into facebook it shows the message "This does not let the app post to facebook" although I am using

LoginManager.getInstance().logInWithPublishPermissions((Activity) context, Arrays.asList("publish_actions"));

enter image description here

On the other hand, how can I check whether user is still logged in fb to call logInWithPublishPermissions just once?

Thank you !

like image 738
BlackBishop Avatar asked Oct 20 '22 07:10

BlackBishop


1 Answers

As said by @Ankush, it is necesary to be registered as an authorized developer at the app settings in the developer facebook web.

My final code (now working fine) is:

public class FbManager {

private Context context;
private static final String LOGTAG = "FbManager";
private CallbackManager callbackManager;

public FbManager(Context context, CallbackManager callbackManager){
    this.context = context;
    this.callbackManager = callbackManager;
}

public static void traceKeyHash(Activity activity){
    try {
        PackageInfo info = activity.getPackageManager().getPackageInfo("com.muv.android", PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.i(LOGTAG, "Share - KeyHash: " + Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

public void share(final String msg) {

    if (isLoggedIn()) {
        post(msg);
    }
    else{
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(LOGTAG, "facebook login success");
                post(msg);
            }

            @Override
            public void onCancel() {
                Log.w(LOGTAG, "facebook login canceled");
            }

            @Override
            public void onError(FacebookException exception) {
                Log.e(LOGTAG, "facebook login error");
                exception.printStackTrace();
            }
        });

        LoginManager.getInstance().logInWithPublishPermissions((Activity) context, Arrays.asList("publish_actions"));
    }
}

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

private void post(final String msg){
    Log.d(LOGTAG, "facebook posting new message");
    Set<String> permissions = AccessToken.getCurrentAccessToken().getPermissions();
    AccessToken accessToken = AccessToken.getCurrentAccessToken();

    Bundle postParams = new Bundle();
    postParams.putString("message", msg);

    GraphRequest request = new GraphRequest(accessToken, "me/feed", postParams, HttpMethod.POST, null);
    GraphRequestAsyncTask asynTaskGraphRequest = new GraphRequestAsyncTask (request);
    asynTaskGraphRequest.execute();
}
}
like image 169
BlackBishop Avatar answered Oct 22 '22 00:10

BlackBishop