Facebook Integration using java

850 Views Asked by At

I am using a facebook4j to send a post on my Facebook wall. While doing so, I am facing an error. The error is showing in my console is when running my code. I have checked my appid, appSecret and accesstoken values which are also correct.

Here is the code I am using to send the message:

public static void  sendMessage(){
        Facebook facebook = new FacebookFactory().getInstance();
        String appId = "XXXXX";
        String appSecret = "XXXXXXXXXXX";
        facebook.setOAuthAppId(appId, appSecret);
        String commaSeparetedPermissions ="user_friends,user_groups,user_photos,user_videos,user_birthday,user_status,user_likes,user_activities,user_location";
        facebook.setOAuthPermissions(commaSeparetedPermissions);
        String accessToken = "XXXXXXXXXXXXXX";
        facebook.setOAuthAccessToken(new AccessToken(accessToken, null));

        try {
            facebook.postStatusMessage("Hello World from Facebook4J From Java Programming....");
        } catch (FacebookException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

I am getting the below error in my console:

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@335678, streamConsumed=true}, errorType=OAuthException, errorMessage=An active access token must be used to query information about the current user., errorCode=2500]
    at facebook4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:189)

Can someone point out my error?

1

There are 1 best solutions below

1
On

This is because you might be using access token which is not valid Or you are not using access token while calling post method. To get a valid active access token you can simply generate by using an active session.

Session session = Session.getActiveSession(); 
if (session != null && session.getState().isOpened()){
 Log.i("sessionToken", session.getAccessToken());
 Log.i("sessionTokenDueDate", session.getExpirationDate().toLocaleString());

Using above generated access token, you can use Graph API call to post on your wall.

Note: Also make sure that you have mentioned the post permission in the Facebook App which you created.