Connecting to Facebook API via Airflow

Author: Omid Vahdaty 19.5.2020

This is a “cut the bullshit and give me what I need” blog .

Prerequisites – Connecting to Facebook API via Airflow

  1. you need permission from facebook business , usually on your own personal facebook user – you need admin!
  2. SDK link: https://developers.facebook.com/docs/marketing-api/sdks/
  3. I picked Facebook SDK – python API
  4. you need an app registered on https://developers.facebook.com/products/ – you need the marketing API selected.
  5. For security, it is recommended that you turn on ‘App Secret Proof for Server API calls’ in your app’s Settings->Advanced page.
  6. get access token from your app’s Settings->Advanced page.
  7. get app secret from app’s Settings->Basic page.
  8. ad account id – from facebook – Ad Account Setup
  9. Verified business account on facebook. verify it on your app.

test via:

curl -X GET "https://graph.facebook.com/oauth/access_token
  ?client_id={your-app-id}
  &client_secret={your-app-secret}
  &grant_type=client_credentials"

setup and environment – Connecting to Facebook API via Airflow

  1. install GCE mahine with debian
  2. install git, pip
  3. git clone the source code of pytho sdk
  4. pip install facebook_busines
pip install facebook_business

run example code:

import sys
sys.path.append('/opt/homebrew/lib/python2.7/site-packages') # Replace this with the place you installed facebookads using pip
sys.path.append('/opt/homebrew/lib/python2.7/site-packages/facebook_business-3.0.0-py2.7.egg-info') # same as above

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount

my_app_id = 'your-app-id'
my_app_secret = 'your-appsecret'
my_access_token = 'your-page-access-token'
FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)
my_account = AdAccount('act_')
campaigns = my_account.get_campaigns()
print(campaigns)

Example 2:

"""
Created on Mon Apr 27 21:37:08 2020

@author: tomerb
"""
from facebook_business.adobjects.adset import AdSet
from facebook_business.adobjects.adsinsights import AdsInsights
from facebook_business.api import FacebookAdsApi


access_token = 'token'
app_secret = 'secret'
ad_account_id = 'act_123456789'
FacebookAdsApi.init(access_token=access_token,app_secret=app_secret)

fields = [
  'impressions','clicks','spend'
]
fields =  ['campaign_name', 'ad_name', 'impressions', 'inline_link_clicks', 'spend']
params = {
        
  'breakdown': 'publisher_platform',
}


print( AdSet(ad_account_id).get_insights(
  fields=fields,
  params=params,
))

Full python example to access facebook marketing api

https://github.com/omidvd79/Big_Data_Demystified/blob/master/facebook_api/marketing_api_example.py

Full python example to access facebook marketing api with group by country and date

https://github.com/omidvd79/Big_Data_Demystified/blob/master/facebook_api/facebook_api_example_with_args.py

Airflow dag: TBD

The facebook api documentation of available fields and params

https://developers.facebook.com/docs/marketing-api/insights/parameters/v6.0

https://developers.facebook.com/docs/marketing-api/insights

https://github.com/facebook/facebook-python-business-sdk/tree/master/examples

https://developers.facebook.com/docs/marketing-api/insights/breakdowns/

https://stackoverflow.com/questions/49658991/get-facebook-marketing-api-ads-insights-results-as-csv-or-json-format


——————————————————————————————————————————

I put a lot of thoughts into these blogs, so I could share the information in a clear and useful way. If you have any comments, thoughts, questions, or you need someone to consult with,

feel free to contact me via LinkedIn:

Leave a Reply

Discover more from Big Data Demystified

Subscribe now to keep reading and get access to the full archive.

Continue reading