Tips and Tricks in a world of Mix



Many times we need the data of social and marketing networks available offline or at least off the network itself. For better BI and retrospective of the data we can export all the data from the Facebook , but how?

In this post I am going to summarize the easy way to connect your Facebook marketing accounts to get the data from the facebook.


  1. Get
  2. The bootstrapping

Meaning we set our facebook account to use with every function we will use against the facebook api – graph facebook.

my_app_id = '<APP_ID>'
my_app_secret = '<APP_SECRET>'
my_access_token = '<ACCESS_TOKEN>'

But how?

Well , I’ve struggled with it quit a bit.

The app_id and app_secret is easy – you just go to the specific app you want to use and copy it from there.

But how to get the token ?

2.a  shows you the token

2.b  The more complicated and strong way is through the

Choose an app


Login and allow to get data


Get user access token


If you don’t have enough permissions facebook will inform you what is needed.

  “error”: {
    “message”: “(#298) Requires extended permission: read_mailbox”,


3.  Go to pycharm. Add config file as config.json.example inside the example folder of  facebook-python-ads-sdk

  "app_id": "<YOUR_APP_ID>",
  "app_secret": "<YOUR_APP_SECRET>",
  "act_id": "act_<YOUR_ACCOUNT_ID>",
  "page_id": "<YOUR_PAGE_ID>",
  "access_token": "<ACCESS_TOKEN>"

4. Go to

Choose the account / business you want to work with

business_id== page_id

Go to


You can see an id of account in the dropbox the act_id or press on the account and see the “act=” value.

Pay attention that you need to add “act_” prefix and then copy paste the id itself.

5. Now inside the Pycharm just right click and debug any of the examples..





AdCampaign to just Campaign in code

from facebookads.objects import (
    AdUser, Campaign,


Code example to get campaigns and insights


from facebookads import FacebookSession
from facebookads import FacebookAdsApi
from facebookads.adobjects.adsinsights import AdsInsights
import datetime

from facebookads.objects import (
    AdUser, Campaign,

import json
import os
import pprint

pp = pprint.PrettyPrinter(indent=4)
this_dir = os.path.dirname(__file__)
config_filename = os.path.join(this_dir, 'config.json')

config_file = open(config_filename)
config = json.load(config_file)

### Setup session and api objects
session = FacebookSession(
api = FacebookAdsApi(session)

if __name__ == '__main__':

    print('\n\n\n********** Reading objects example. **********\n')

    ### Setup user and read the object from the server
    me = AdUser(fbid='me')

    ### Read user permissions
    print('>>> Reading permissions field of user:')


    ### Get first account connected to the user
    my_account = me.get_ad_account()

    ### Read connections (in this case, the accounts connected to me)

    # Pro tip: Use list(me.get_ad_accounts()) to make a list out of
    # all the elements out of the iterator

    my_accounts_iterator = me.get_ad_accounts()#[0]
    print('>>> Reading accounts associated with user')
    for account in my_accounts_iterator:
        for campaign in account.get_campaigns(fields=[]):
            print(">>> Campaign Stats")
            campaignObj = Campaign(campaign._json[u'id'])

            today =
            start_time = str(today - datetime.timedelta(weeks=1))
            end_time = str(today)

            campaign = Campaign(campaign._json[u'id'])
            params = {
                'time_range': {
                    'since': start_time,
                    'until': end_time,
                'fields': [
            insights = campaign.get_insights(params=params)


This slideshow requires JavaScript.





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Tag Cloud

%d bloggers like this: