我正在使用来自 Facebook 的营销 API 的 Java SDK。我在阅读 Insights 时遇到问题。我调用我帐户的广告集,并为每个广告集调用 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());
}
}
我只想获得在我指定的日期范围内至少有一次展示的广告组。或者至少在一个电话中获得所有见解。谢谢!