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
- you need permission from facebook business , usually on your own personal facebook user – you need admin!
- SDK link: https://developers.facebook.com/docs/marketing-api/sdks/
- I picked Facebook SDK – python API
- you need an app registered on https://developers.facebook.com/products/ – you need the marketing API selected.
- For security, it is recommended that you turn on ‘App Secret Proof for Server API calls’ in your app’s Settings->Advanced page.
- get access token from your app’s Settings->Advanced page.
- get app secret from app’s Settings->Basic page.
- ad account id – from facebook – Ad Account Setup
- 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
- install GCE mahine with debian
- install git, pip
- git clone the source code of pytho sdk
- 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
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/
——————————————————————————————————————————
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: