skip to Main Content

I have to develop an automated process for fetching my Facebook page insights. Since, Access token used for authentication purpose is temporary in nature. Therefore, I created a Facebook App and by providing all required permissions I generated a Page Access Token so that I could extend it’s life-span.
Reference Link : Java + RestFB API: Getting fresh Page Access Token without messing with AppID, appSecret

The following is my piece of code :

    FacebookClient fb=new DefaultFacebookClient(accesstoken,Version.VERSION_2_7);
    Connection<Insight> insights =fb.fetchConnection("119456244790112/insights", Insight.class,Parameter.with("since", "2016-08-01"),Parameter.with("until", "2016-08-27"));
    for (Insight insight : insights.getData())
            if(insight.getName().equals("page_impressions") && (insight.getPeriod().equals("day")) )
                System.out.println(insight.getName()+"t"+insight.getPeriod()+"t"+insight.getValues());

‘accesstoken’ is the short-life Page access-token obtained by following the mentioned link.

The following is the Exception Stack I’m getting :

Exception in thread “main”
com.restfb.exception.FacebookOAuthException: Received Facebook error
response of type OAuthException: Invalid query (code 3001, subcode
1504028) at
com.restfb.DefaultFacebookClient$DefaultGraphFacebookExceptionMapper.exceptionForTypeAndMessage(DefaultFacebookClient.java:1191)
at
com.restfb.DefaultFacebookClient.throwFacebookResponseStatusExceptionIfNecessary(DefaultFacebookClient.java:1117)
at
com.restfb.DefaultFacebookClient.makeRequestAndProcessResponse(DefaultFacebookClient.java:1058)
at
com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:969)
at
com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:931)
at
com.restfb.DefaultFacebookClient.fetchConnection(DefaultFacebookClient.java:356)
at Main.main(Main.java:31)

Please help me finding the Page Insights using Page Access Token and hence extending it’s life-span so that I could produce an automated process out of this. Thanks !

2

Answers


  1. Chosen as BEST ANSWER

    Finally I got the success in pulling the Page Insights data using the 'Page Access Token' with extended life-span.

    The following is the snippet for making the appropriate requests to Facebook :

    Connection<Insight> insights =fbclient.fetchConnection("1388062484542431/insights/page_views/day", Insight.class,Parameter.with("since", "2016-08-01"),Parameter.with("until", "2016-08-30"));
    

    If multiple Metrics need to be passed then we can do so by passing comma-separated names of the required Metrics.

    Thanks.


  2. You asked two questions in one.

    1. Extending an access token:
      First, you need the user access token. Than extend it (Check the RestFB manual: http://restfb.com/documentation/#access-token-extend) and take the page access token for the page from /me/accounts. The page access token has an unlimited lifetime.

    2. The OAuthException:
      If you look at the error message of the exception you should see that the metric is missing. You nee to tell Facebook what metric you like to get. Check the Graph API Reference for valid metrics: https://developers.facebook.com/docs/graph-api/reference/v2.7/insights

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search