In the FB marketing api, there is an option to export_format: 'csv', how do I download the csv file?

末鹿安然 提交于 2020-01-03 18:43:12

问题


I'm looking to download a CSV from my api call to FB ads, I've set it in the parameters as per the code below, the call works fine but I only get a paginated JSON as a response. I'm not quite sure how to access the CSV report, the response doenst give me any report ID or download link. How can I download this CSV?

Using nodejs facebook-nodejs-ads-sdk image showing the parameter information 'export_format' Documentation: https://developers.facebook.com/docs/marketing-api/reference/adgroup/insights/


var getReport = function(){
  console.log("API for FB report has been called")
  let ads_insights;
  let ads_insights_id;

  const logApiCallResult = (apiCallName, data) => {
    console.log(apiCallName);
    if (showDebugingInfo) {
      console.log('Data:' + JSON.stringify(data));
    }
  };

  const fields = [
    'ad_name',
    'impressions',
    'reach',
    'spend'
  ];

  const params = {
    'export_format': 'csv',
    'export_name': new Date() + 'fb-download',
    'level' : 'ad',
    'filtering' : [],
    'breakdowns' : [],
    'time_range' : {'since':'2018-01-22','until':'2018-01-23'}
  };

  new AdAccount(ad_account_id).getInsights(
    fields,
    params).then((result) => {
    logApiCallResult('ads_insights api call complete.', result);
    console.log("##########" + result + "#####")
    ads_insights_id = result[0].id;
  }).catch((error) => {
    console.log(error);
  });
}

回答1:


Exporting actual report files doesn't seem to be a part of the official Facebook Graph API.

My solution was to make a GET request to the following endpoint:

https://www.facebook.com/ads/ads_insights/export_report/?report_run_id=<REPORT_RUN_ID>&name=<REPORT_FILE_NAME>&format=csv&access_token=<FACEBOOK_API_KEY> 

Please note that this endpoint will output a file with columns that constantly change without warning, so its not good for automated data collection. The only way to export standardized reports is in the JSON format.

The docs for this endpoint are sort of hard to find: located here.



来源:https://stackoverflow.com/questions/48410866/in-the-fb-marketing-api-there-is-an-option-to-export-format-csv-how-do-i-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!