Two newbie questions

685 views
Skip to first unread message

Mark Conder

unread,
Jan 25, 2014, 2:24:24 PM1/25/14
to faceb...@googlegroups.com
First, thank you for the great library, it has helped me a lot.

I have a java app that I want the user to be able to post to my facebook pages.

Question 1: I can successfully connect and post to my FB page using an access token.But when I try to use my app token, I get the following error:

[Sat Jan 25 13:17:18 CST 2014]Request: 
[Sat Jan 25 13:17:18 CST 2014]POST https://graph.facebook.com/me/feed
[Sat Jan 25 13:17:18 CST 2014]Accept-Encoding: gzip
[Sat Jan 25 13:17:18 CST 2014]User-Agent: facebook4j http://facebook4j.org/ /2.0.4
[Sat Jan 25 13:17:18 CST 2014]Post Params: message=Hello%20World%20from%20Facebook4J.&access_token=6...U
[Sat Jan 25 13:17:19 CST 2014]Response: 
[Sat Jan 25 13:17:19 CST 2014]HTTP/1.1 400 Bad Request
[Sat Jan 25 13:17:19 CST 2014]X-FB-Debug: /dzL/xNB/x3Vj0IQgoJvPzL+ykpjxCoTkOEttumIUyQ=
[Sat Jan 25 13:17:19 CST 2014]WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "An active access token must be used to query information about the current user."
[Sat Jan 25 13:17:19 CST 2014]Date: Sat, 25 Jan 2014 19:17:19 GMT
[Sat Jan 25 13:17:19 CST 2014]Access-Control-Allow-Origin: *
[Sat Jan 25 13:17:19 CST 2014]Content-Length: 140
[Sat Jan 25 13:17:19 CST 2014]Expires: Sat, 01 Jan 2000 00:00:00 GMT
[Sat Jan 25 13:17:19 CST 2014]Connection: keep-alive
[Sat Jan 25 13:17:19 CST 2014]X-FB-Rev: 1095454
[Sat Jan 25 13:17:19 CST 2014]Content-Type: application/json; charset=UTF-8
[Sat Jan 25 13:17:19 CST 2014]Pragma: no-cache
[Sat Jan 25 13:17:19 CST 2014]Cache-Control: no-store
[Sat Jan 25 13:17:19 CST 2014]{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}

FacebookException{statusCode=400, response=HttpResponse{statusCode=400, responseAsString='{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
', is=sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@28cb43a9, streamConsumed=true}, errorType='OAuthException', errorMessage='An active access token must be used to query information about the current user.', errorCode=2500, errorSubcode=-1}
at facebook4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:189)

I am using the following code:
Facebook facebook = new FacebookFactory().getInstance();
try { facebook.postStatusMessage("Hello World from Facebook4J.");
And oauth.accessToken ikn facebook4j.properties references my app token.

I want to use a app token so that it is permanent. What am I doing wrong?

Question 2: I can only post to my default FB page. How to I specify one of my other pages to post to? I think I need to find the page_ID in graph explorer, but once I find it how do I use it in facebook4j?

Thanks,
Mark

Mark Conder

unread,
Jan 25, 2014, 11:55:32 PM1/25/14
to faceb...@googlegroups.com
My new code:

Facebook facebook = new FacebookFactory().getInstance();
   PostUpdate post = null;
   try {
      post = new PostUpdate(new URL("http://facebook4j.org"))
         .picture(new URL("http://facebook4j.org/images/hero.png"))
         .name("test")
         .caption("another test")
         .description("this is a test");
   } catch (MalformedURLException e2) {
   // TODO Auto-generated catch block
      e2.printStackTrace();
   }
   try {
//    facebook.postStatusMessage("Hello World from Facebook4J.");
      facebook.postFeed("page_id or user_id",post);
   } catch (FacebookException e1) {
   // TODO Auto-generated catch block
      e1.printStackTrace();
   }

This posts fine if I use my personal user_id. But if, I try to use my other user_id or page_id, it does not work (no errors, just doesn't post)

Ryuji Yamashita

unread,
Jan 26, 2014, 8:49:54 PM1/26/14
to faceb...@googlegroups.com
Hi,
Thanks for using Facebook4J.

Question 1)
You cannot publish a post to a page using app access token.
A page access token with publish_actions permission is required.

You can get the page access token as follows:


Question 2)
You can find your page ids via Facebook#getAccounts() method.


Best,

2014年1月26日日曜日 4時24分24秒 UTC+9 Mark Conder:

Mark Conder

unread,
Jan 26, 2014, 10:49:08 PM1/26/14
to faceb...@googlegroups.com
Thanks for your reply. With your help I was able to find my page IDs, and can post when I get a page access token manually from graph explorer. But I am missing one thing for my program, I don't know how to get my page access token without first logging in - and I can't get an Facebook instance without the OAuthAccessToken.  Here is my code - it gives me a responseAsString='{"error":{"message":"Unsupported get request.","type":"GraphMethodException","code":100}} error.

   public void actionPerformed(ActionEvent e) {
    if(e.getSource()==btnPostToFB) {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
    .setOAuthAppId("1***")
    .setOAuthAppSecret("9***")
                                 .setOAuthAccessToken("C***")
    .setOAuthPermissions("email,publish_stream,manage_pages");
    FacebookFactory ff = new FacebookFactory(cb.build());
    Facebook facebook = ff.getInstance();
   ResponseList<Account> accounts = null;
try {
accounts = facebook.getAccounts("1***");
} catch (FacebookException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
    Account yourPageAccount = accounts.get(0);  // if index 0 is your page account.
    String pageAccessToken = yourPageAccount.getAccessToken();
   facebook.setOAuthAccessToken(new AccessToken(pageAccessToken, null));
    PostUpdate post = null;
try {
post = new PostUpdate(new URL("http://facebook4j.org"))
   .picture(new URL("http://facebook4j.org/images/hero.png"))
   .name("test")
   .caption("another test")
   .description("this is a just a test");
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
      try {
// facebook.postStatusMessage("Hello World from Facebook4J.");
    facebook.postFeed("107246956023241",post);
  } catch (FacebookException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
  }
       }
      }

Do I need to get a User Access token first? If I remove the user access token from .setOAuthAccessToken in the configuration builder then I get a credentials error. 
Reply all
Reply to author
Forward
0 new messages