Iterating over PagedList result retrieved from the Facebook Graph API

863 views Asked by At

Trying to retrieve all marketing accounts of a Facebook business account using the spring social framework for Facebook .

  1. I want to know if there is any better way to it other than what i did below ?
  2. Does what i did for paging the query result is the right way to do it ?
  3. Is there another way to retrieve data from the Facebook Marketing API ?


I really appreciate any help you can provide.

public List<String> getListAdAccountsId(String businessAccountId) {
    final List<String> accountsId = new ArrayList<String>();
    final MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<String, String>();
    queryParameters.add("fields", "account_id");
    queryParameters.add("offset", "0");
    PagedList<Map> pagedResultSubSet = facebook.fetchConnections(businessAccountId, "adaccounts", Map.class,
            queryParameters);
    do {
        queryParameters.set("offset", pagedResultSubSet.getNextPage().getOffset().toString());
        pagedResultSubSet = facebook.fetchConnections(businessAccountId, "adaccounts", Map.class, queryParameters);
        accountsId.addAll(pagedResultSubSet.parallelStream().map(e -> e.get("id").toString())
                .collect(Collectors.toList()));
    } while (pagedResultSubSet.getNextPage() != null);

    return accountsId;
}
0

There are 0 answers