AdSets API呼叫Facebook

时间:2018-07-17 10:52:18

标签: facebook-marketing-api facebook-java-api

我正在使用Java SDK从Facebook营销API。我在阅读见解时遇到问题。我调用帐户的广告集,并为每个广告集调用getInsights()函数。在请求了解两个不同的广告集之间,我有一个Thread.sleep(5000)。但是,我仍然收到错误17:“达到用户请求限制”。我使用以下代码:

public static final APIContext context = new APIContext(
            "ACCESS TOKEN",
            "SECRET KEY"
    );
public static void main(String[] args) throws APIException, ParseException
{

    String sDate1="2018-04-01"; 
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    Calendar c = Calendar.getInstance();
    String finala="2018-07-16";  
    Date datefinal=new SimpleDateFormat("yyyy-MM-dd").parse(finala); 
    System.out.println(datefinal);
    c.setTime(df.parse(sDate1));

    sDate1=df.format(c.getTime());
    System.out.println(c.getTime());

    while(!datefinal.before(c.getTime())) {
            System.out.println(df.format(c.getTime()));
    try {   
        AdAccount account = new AdAccount("ACCOUNT ID", context);
            try {
            APINodeList<AdSet> adsets = account.getAdSets().setTimeRange("{\"since\":\""+sDate1+"\",\"until\":\""+sDate1+"\"}").requestAllFields().execute();
            adsets = adsets.withAutoPaginationIterator(true);
            for(AdSet adset : adsets) {
                System.out.println(adset.getFieldName());
                Thread.sleep(5000);
                //System.out.println(adset.getFieldId());
                APINodeList<AdsInsights> adsInsightss = adset.getInsights()
                      .setTimeRange("{\"since\":\""+sDate1+"\",\"until\":\""+sDate1+"\"}")
                      .requestField("campaign_name")
                      .requestField("clicks")
                      .requestField("cpc")
                      .requestField("cpm")
                      .requestField("cpp")
                      .requestField("ctr")
                      .requestField("date_start")
                      .requestField("date_stop")
                      .requestField("frequency")
                      .requestField("impressions")
                      .requestField("reach")
                      .requestField("spend")
                      .requestField("unique_clicks")
                      .requestField("unique_ctr")
                      .requestField("website_ctr")
                      .requestField("website_purchase_roas")
                      .execute();
                if(adsInsightss.size()>0) {
                    System.out.println(adset.getFieldName());

                    }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    c.setTime(df.parse(sDate1));
    c.add(Calendar.DATE, 1);

    sDate1=df.format(c.getTime());

    }
}

我只想获得在我指定的日期范围内至少获得一次展示的广告集。或者至少一次调用即可获得所有见解。 谢谢!

1 个答案:

答案 0 :(得分:0)

我通过将try块中的代码替换为:

APINodeList<AdsInsights> adsInsightss = account.getInsights().setTimeRange("{\"since\":\""+sDate1+"\",\"until\":\""+sDate1+"\"}").setParam("level", "ad").setParam("limit", 1000).requestField("adset_name")
                          .requestField("campaign_name")
                          .requestField("clicks")
                          .requestField("cpc")
                          .requestField("cpm")
                          .requestField("cpp")
                          .requestField("ctr")
                          .requestField("date_start")
                          .requestField("date_stop")
                          .requestField("frequency")
                          .requestField("impressions")
                          .requestField("reach")
                          .requestField("spend")
                          .requestField("unique_clicks")
                          .requestField("unique_ctr")
                          .requestField("website_ctr")
                          .requestField("website_purchase_roas")
                          .requestField("action_values").execute();