0

Im trying to post some status message on my facebook page, not my personal facebook profile account, but on page i created separately.

My current code looks like this:

import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.Post;
import facebook4j.ResponseList;
import facebook4j.conf.Configuration;
import facebook4j.conf.ConfigurationBuilder;

public class FacebookImpl {

// from https://developers.facebook.com/apps/
static String appId = "11removed";

// from https://developers.facebook.com/apps/
static String appSecret = "c0removed";

// from https://developers.facebook.com/tools/accesstoken/
static String appToken = "11removed";

// my facebook page
static String myFaceBookPage = "niremoved";

public static void main(String[] args) throws FacebookException {

    // Make the configuration builder
    ConfigurationBuilder confBuilder = new ConfigurationBuilder();
    confBuilder.setDebugEnabled(true);

    // Set application id, secret key and access token
    confBuilder.setOAuthAppId(appId);
    confBuilder.setOAuthAppSecret(appSecret);
    confBuilder.setOAuthAccessToken(appToken);

    // Set permission
    // https://developers.facebook.com/docs/facebook-login/permissions
    confBuilder.setOAuthPermissions("manage_pages,publish_pages,publish_actions");
    confBuilder.setUseSSL(true);
    confBuilder.setJSONStoreEnabled(true);

    // Create configuration object
    Configuration configuration = confBuilder.build();

    // Create FacebookFactory and Facebook instance
    FacebookFactory ff = new FacebookFactory(configuration);
    Facebook facebook = ff.getInstance();

    // this one works fine
    System.out.println(getFacebookPostes(facebook, myFaceBookPage));

    // try to post status
    // FacebookException{statusCode=403, errorType='OAuthException', 
    // errorMessage='(#200) The user hasn't authorized the application to perform this action', errorCode=200, errorSubcode=-1, version=2.4.5}
    facebook.postStatusMessage(myFaceBookPage, "Test Facebook4J.");

}

public static String getFacebookPostes(Facebook facebook, String page) throws FacebookException {
    ResponseList<Post> results = facebook.getPosts(page);
    // as example just to see if i get any data
    return results.get(0).getMessage();
}

}

Problem is that i cant post any message on page using this code: facebook.postStatusMessage(myFaceBookPage, "Test Facebook4J."); but i can get messages already posted (via facebook web interfaces) with method getFacebookPostes.

Can anyone help me with this one ? And please do not paste some random dev.Facebook link to look into API.

What i did: - create app on https://developers.facebook.com/apps/ i have appid, appsecret

Thanks

anotherUser
  • 551
  • 6
  • 29
  • "errorMessage='(#200) **The user hasn't authorized the application to perform this action**'" - The error message says it all !? You need to grant the app the right to use that function. Cannot tell you how to do this with FB API, though. – Fildor Jun 30 '16 at 12:00
  • Thanks for your reply. Yes it does, it says crystal clear whats the problem, but i don't know if im supposed to setup something via developer.facebook web UI or via code ? – anotherUser Jun 30 '16 at 12:18
  • You need to provide the permissions the app needs in code. Then the user will be prompted to verify and allow the app to use those functions. Next time, the app (or better the token) will be known to have the rights needed. – Fildor Jun 30 '16 at 12:44
  • I guess you need another entry to this : `confBuilder.setOAuthPermissions("manage_pages,publish_pages,publish_actions");` – Fildor Jun 30 '16 at 12:53
  • Which one ? I tried with manage_pages,publish_pages,publish_actions,pages_show_list but its same :) And to be clear, this is all my code, i do not have any user interaction, so part of your comment "Then the user will be prompted to verify and allow the app to use those functions" does not work for me. – anotherUser Jun 30 '16 at 12:58
  • Can't tell. I just know from OAuth in google API that it is working that way. Haven't worked with FB API. How did you confirm the other permissions? Through some kind of developer console? – Fildor Jun 30 '16 at 13:29
  • Have you tried "user_status"? – Fildor Jun 30 '16 at 13:38
  • Yes i tried with user_status but its same ... – anotherUser Jun 30 '16 at 14:16

0 Answers0