pythonfacebookfacebook-marketing-api

Facebook Business API request campaigns via python


We are busy upgrading from the Marketing API to the Business API (not graph API and not marketing API) for Facebook. The documentation states that one should be able to create a test.py with the following code:

import sys
sys.path.append('/opt/homebrew/lib/python2.7/site-packages') 
sys.path.append('/opt/homebrew/lib/python2.7/site-packages/facebook_business-3.0.0-py2.7.egg-info') 

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-access-token'
FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)
my_account = AdAccount('your-adaccount-id')
campaigns = my_account.get_campaigns()
print(campaigns)

This requires an app secret proof which I am obtaining with the following hash:

facebook_app_id     = 'xxxxx'
facebook_app_secret = 'xxxxx'
facebook_app_token  = 'xxxxx|xxxxx'.format(facebook_app_id,facebook_app_secret)

import hmac,hashlib
app_secret_proof    = hmac.new(facebook_app_secret.encode('utf-8'),
                       msg=facebook_app_token.encode('utf-8'),
                       digestmod=hashlib.sha256).hexdigest()

print(app_secret_proof)

The is the error I get:

Traceback (most recent call last):
File "test.py", line 12, in <module>
FacebookAdsApi.init(my_app_id,my_app_secret,my_access_token,my_appsecret_proof)
File "/Users/facebook_business/api.py", line 202, in init
cls.set_default_account_id(account_id)
File "/Users/facebook_business/api.py", line 225, in set_default_account_id
"Account ID provided in FacebookAdsApi.set_default_account_id " 
ValueError: Account ID provided in FacebookAdsApi.set_default_account_id expects a string that begins with 'act_'

None of the ID's start with act_, I am currently using the App ID that is at the top left of the app dashboard which one should I be using? Marketing API


Solution

  • The Ad account number in business manager is obtained on the front log in screen.

    From Business Manager