Download statistics of Facebook ads in the background (without a web browser)

I am developing a server application backoffice, which is supposed to send our customers statistics on advertising campaign statistics, in which packages are tied to various providers, one of which is Facebook.

Now the problem is how to download specific advertising campaigns from Facebook. Basically, I need a table for a specific campaign with values ​​for impressions and clicks for each day from a given range.

One of the problems is that the application is a background server process that runs periodically without user interaction, so I suspect there may be an authentication problem.

From what I have read so far, I believe that I should

  • register my application on facebook
  • used to access the ads API (?)
  • use either the outdated REST API to get statistics, or maybe adsstatistics from the api chart (but I believe that the format there is not divided into separate days).

How can I authenticate in this case?

Any suggestion as to what is the right solution here?

(Note: I used to do similar ones for Google, where I successfully used http://code.google.com/intl/cs/apis/adwords/docs/guides/reporting.html - this is just for reference on what I need reach).

+4
source share
1 answer

Check out this authentication page when you have access to the AdWords API: http://developers.facebook.com/docs/authentication/

In the end, you will need access_token, which depends on your application and the Facebook account you are accessing.

Here is our process:

  • Log in to your Facebook account, which contains ad data.
  • Paste this into this browser address bar: https://www.facebook.com/dialog/oauth?client_id={your_application_id}&scope=ads_management,offline_access,read_insights&redirect_uri={your website}
  • You should go to a page that allows you to authorize your application for this Facebook account (green Allow )
  • Copy the authorization code that appears after the = code in the redirected URL
  • Paste this into this browser address bar (you may not have client_secret with your application if you do not try it without client_secret): https://graph.facebook.com/oauth/access_token?client_id={your_application_id}&redirect_uri={your website}&client_secret={your application secret}&code={code you got from step 4}
  • You should go to the page containing access_token

I don’t think you need to save the authorization code, since access_token should not expire (if you requested offline_access), if the log information has not been changed for your Facebook account.

Downloading statistics To search for statistics, I would not use the outdated REST API, since Facebook will depreciate it. The Graph API allows you to automatically get statistics using:

GET https://graph.facebook.com/stats/{starttime}/{endtime}/stats?ids={campaign_id}&access_token=...

start time and end time can be in the format YYYY-MM-DD HH: MM: SS or as unix time (epoch?)

+4
source

Source: https://habr.com/ru/post/1389229/


All Articles