Insights Example

63 views
Skip to first unread message

Samir Issa

unread,
Sep 22, 2017, 6:50:58 PM9/22/17
to RestFB

Hi ,

how could I reproduce this, 


I always get error :

Exception in thread "main" com.restfb.exception.FacebookOAuthException: Received Facebook error response of type OAuthException: An access token is required to request this resource. (code 104, subcode null)
at com.restfb.exception.generator.DefaultFacebookExceptionGenerator$DefaultGraphFacebookExceptionMapper.exceptionForTypeAndMessage(DefaultFacebookExceptionGenerator.java:174)
at com.restfb.exception.generator.DefaultFacebookExceptionGenerator.throwFacebookResponseStatusExceptionIfNecessary(DefaultFacebookExceptionGenerator.java:59)
at com.restfb.DefaultFacebookClient.makeRequestAndProcessResponse(DefaultFacebookClient.java:987)
at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:909)
at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:871)
at com.restfb.DefaultFacebookClient.fetchConnection(DefaultFacebookClient.java:372)
at com.silversharksolutions.facebookapirest.GraphRead.getInsights(GraphRead.java:139)
at com.silversharksolutions.facebookapirest.GraphRead.run(GraphRead.java:80)
at com.silversharksolutions.facebookapirest.GraphRead.main(GraphRead.java:68)




My code  :



   public void getInsights() {
       
     
     facebookClient29 = new DefaultFacebookClient("ACCESS_KEY", Version.VERSION_2_9);

                Connection<Insight> insights = facebookClient29.fetchConnection(
                        "999577163512356/insights/?metric=page_fan_adds_unique,page_fan_adds", 
                        Insight.class);
                int count = 0;
                for (List<Insight> insight : insights) {
                  for (Insight insight1 : insight) {
                      //id,created_time,message,from
                        LOG.info("Insight Id: " + insight1.getPeriod() );
                        LOG.info("Insight Title: " + insight1.getName());
                        
                        count++;
                    }

                }
                LOG.info("Total Attachments: " + count);

    }


Norbert Bartels

unread,
Sep 24, 2017, 7:14:07 AM9/24/17
to RestFB

Hi Samir,

please try this code:

facebookClient29 = new DefaultFacebookClient("ACCESS_KEY"
, Version.VERSION_2_9);

Connection<Insight> insights = facebookClient29.fetchConnection(
                        "999577163512356/insights", 
                        Insight.class, 
                        Parameter.with("metric", "page_fan_adds_unique,page_fan_adds"));

int count = 0;
for (List<Insight> insightList : insights) {
    for (Insight insight : insightList) {
        LOG.info("Insight Id: " + insight.getPeriod());
        LOG.info("Insight Title: " + insight.getName());

        count++;
     }
}
LOG.info("Total Insights: " + count);

This is not tested, but gives an idea how it should work. BTW if this is the solution (and I think so :)) you can drop a short note, because I add this important information to our documentation for other developers running in this problem.

Cheers
Norbert

Samir Issa

unread,
Sep 25, 2017, 2:40:33 PM9/25/17
to RestFB
hi , 

It works fine , just change to this data :

                    LOG.info("Insight Name: " + insight.getName());
                    LOG.info("Insight Period: " + insight.getPeriod());
                    List<JsonObject> json = insight.getValues();
                    LOG.info("value :"+json.get(0).get("value"));
                    LOG.info("end_time :"+json.get(0).get("end_time"));
                    LOG.info("value :"+json.get(1).get("value"));
                    LOG.info("end_time :"+json.get(1).get("end_time"));
                    LOG.info("Insight Tittle: " + insight.getTitle());

Are these insights not yet mapped in the API?

thank you

Norbert Bartels

unread,
Sep 25, 2017, 3:40:59 PM9/25/17
to RestFB
Hi Samir, 

There is no description of the "InsightResult" in the Graph API reference, so we have to reverse engineer the structure. Therefore we used the less error-prone way and return JsonObject lists.

Feel free to provide some example and we can built types for these, too.

Cheers,
Norbert
Reply all
Reply to author
Forward
0 new messages