将状态消息发布到Facebook页面

时间:2016-06-30 11:52:30

标签: java facebook4j

我试图在我的Facebook页面上发布一些状态消息,而不是我个人的Facebook个人资料帐户,但是在我单独创建的页面上。

我目前的代码如下:

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();
}

}

问题是我无法使用此代码在页面上发布任何消息:facebook.postStatusMessage(myFaceBookPage, "Test Facebook4J.");但我可以使用方法getFacebookPostes获取已发布的消息(通过Facebook网络界面)。

任何人都可以帮我这个吗?请不要粘贴一些随机的dev.Facebook链接来查看API。

我做了什么: - 在https://developers.facebook.com/apps/上创建app我有appid,appsecret

由于

0 个答案:

没有答案