如何在不中断Facebook Graph API调用的情况下获取数据?

时间:2019-04-05 16:36:47

标签: facebook facebook-graph-api

我需要做些什么来规避这个问题,因为当我请求数据2个月时,我已经收到此错误,每天休息的时候,我会打以下电话。使用Little Data可以完美工作,但是当我增加服务期限时,服务器会带给我

已达到用户请求限制”,“类型”:“ OAuthException”,“ is_transient”:true,“代码”:17,“错误子代码”:2446079,“ fbtrace_id”:“ ...

function solicitacaoAssicrona(){
 var service = getService()
 var batch = [{"method": "GET", "relative_url":"v3.2/act_1125789557444919/insights/impressions,reach,frequency,spend,campaign_name,account_name,clicks,cost_per_10_sec_video_view,cpm,cpp?level=campaign&since=2016-03-03&until=2019-03-04&time_increment=1&limit=100"}]
// var batchUrl = encodeURIComponent(JSON.stringify(batch));
// Logger.log(batchUrl);
 var url = "https://graph.facebook.com?include_headers=false&batch=" + encodeURIComponent(JSON.stringify(batch)) 
 var response = UrlFetchApp.fetch(url, {
   method: 'POST',
   headers: {    
     Authorization: 'Bearer ' + service.getAccessToken()
   }
 });
 var result =  JSON.parse(response.getContentText()); 
 Logger.log(result)
// response.forEach(function(resp){
// var resp = JSON.parse(resp.body);
// //Logger.log(JSON.stringify(resp, null, 2));
//   
////   resp.data[0].values.forEach(function(response){
////     
////   
////   }) 
//// 
// })
}

我看了看文档,但到目前为止没有成功! https://developers.facebook.com/docs/marketing-api/insights/best-practices/

那是我打的电话

var metricas = [
'impressions',
'reach',
'unique_clicks',
'account_currency',
'account_id',
'account_name',
'ad_id',
'ad_name',
'adset_id',
'adset_name',
'buying_type',
'campaign_id',
'campaign_name',
'clicks',
'cost_per_inline_link_click',
'cost_per_inline_post_engagement',
'cost_per_unique_click',
'cost_per_unique_inline_link_click',
'cpc', 
'cpm',
'cpp',
'ctr',
'date_start',
//'date_stop',
'frequency',
'inline_link_click_ctr',
'inline_link_clicks',
'inline_post_engagement',
'objective',
'relevance_score',
'social_spend',
'spend',
'unique_ctr',
'unique_inline_link_click_ctr',
'unique_inline_link_clicks',
'unique_link_clicks_ctr',
//'website_ctr',
'video_10_sec_watched_actions',
'cost_per_10_sec_video_view',
'video_30_sec_watched_actions',
'video_avg_percent_watched_actions',
'video_avg_time_watched_actions',
'video_p100_watched_actions',
'video_p25_watched_actions',
'video_p50_watched_actions',
'video_p75_watched_actions',
'video_play_actions',
'video_thruplay_watched_actions',
'video_p95_watched_actions', 
]

var parameters = metricas.join(',');
var url = 'https://graph.facebook.com/v3.2/act_xxxxxxxxxx/insights?fields= + parameters + '&level=ad&time_range[since]=2019-02-05&time_range[until]=2019-04-05&time_increment=1&limit=200'

1 个答案:

答案 0 :(得分:0)

与批处理请求中可以检索的数据量有关。对于较长的时间段,应将其分成多个较小的块,并按顺序排列,从而检索多个请求中所需的数据。看这个例子:

Code.gs

从文件的第88行,您可以看到如何将其划分为多个请求。 https://github.com/halsandr/Facebook_Connector/blob/master/Code.gs

function dateDelta(dObj, num) {
    if (isNaN(num)) {
      var dateStart = new Date(dObj);
    } else {
      var dateStart = new Date(dObj);
      var dateStart = new Date(dateStart.setDate(dateStart.getDate() + num));
    }
    var dd = dateStart.getDate();
    var mm = dateStart.getMonth()+1; //January is 0!

    var yyyy = dateStart.getFullYear();
    if(dd<10){
        dd='0'+dd;
    } 
    if(mm<10){
        mm='0'+mm;
    } 
    var dateStart = yyyy + "-" + mm + "-" + dd;
    return dateStart;
  }

  var gStartDate = new Date(request.dateRange.startDate);
  var gStartDate = new Date(dateDelta(gStartDate, -1));
  var gEndDate = new Date(request.dateRange.endDate);
  var gEndDate = new Date(dateDelta(gEndDate, +1));
  var gRange = Math.ceil(Math.abs(gEndDate - gStartDate) / (1000 * 3600 * 24));
  var gBatches = Math.ceil(gRange / 92);

  if (gBatches < 2) {
    var batch = [{"method": "GET", "relative_url": request.configParams.pageID + "/insights/page_fans,page_impressions,page_post_engagements?since=" + dateDelta(gStartDate) + "&until=" + dateDelta(gEndDate)}];
    //console.log(batch);
  } else {
    batch = [];
    var iterRanges = gRange / gBatches;

    for (i = 0; i < gBatches; i++) {
      var iterStart = dateDelta(gStartDate, (iterRanges * i));
      if (i == (gBatches - 1)) {
        var iterEnd = dateDelta(gEndDate);
      } else {
        var iterEnd = dateDelta(gStartDate, (iterRanges * (i + 1)) + 1);
      }
      batch.push({"method": "GET", "relative_url": request.configParams.pageID + "/insights/page_fans,page_impressions,page_post_engagements?since=" + iterStart + "&until=" + iterEnd})
    }
    //console.log(batch);
  }

    // Fetch the data with UrlFetchApp
  var url = "https://graph.facebook.com?include_headers=false&batch=" + encodeURIComponent(JSON.stringify(batch))