自动化获取Facebook Ads数据:如何获取account_id与access_token?
实现无交互自动化下载Facebook广告数据的解决方案
Great question! Facebook's server-side automation flow is indeed different from Google's service account model, but it's totally possible to set up a fully automated pipeline without user interaction. Let's walk through exactly how to get your account_id and a permanent access token, then integrate them with your Airflow plugin.
1. 获取你的Ad Account ID
There are two easy ways to get this:
- Via Facebook Ads Manager: Log into your Ads Manager, click the account dropdown in the top-right corner, select "Account Settings". Your Account ID (a numeric string) will be displayed prominently—you'll need to format it as
act_{your_account_id}when using it in the Graph API. - Via Graph API (if you have a temporary token): If you already have a short-lived user access token, call the
GET /me/adaccountsendpoint. The response will include a list of your ad accounts, each with anidfield that's already in theact_XXXXXXformat you need.
2. 生成无交互的长期Access Token
Facebook doesn't use JSON service account files, but you can create a system user access token that works permanently (no user login required). Here's how:
Step 1: Create a System User
- Go to your Facebook Business Settings. Under the "Users" menu, select "System Users".
- Click "Add", choose the "Admin" role (you'll need full permissions for your Business Manager), and name your system user (e.g., "Airflow Automation Bot").
Step 2: Assign Ad Account Permissions
- In Business Settings, navigate to "Accounts" > "Ad Accounts".
- Find your target ad account, click "Assign Partners or People", and add the system user you just created. Grant it at least the "Advertiser" role (or "Admin" for full access) to ensure it can read ad insights.
Step 3: Generate and Extend the Token
- Back in the System Users page, locate your bot user and click "Generate Token".
- Select your Business Manager from the dropdown, then check the permissions you need:
ads_read(required for pulling insights data)business_management(optional, but helpful for account management tasks)
- The initial token is short-lived (60 days). To extend it to a permanent one, call this Graph API endpoint:
GET /{system_user_id}/access_tokens?access_token={short_lived_token}&grant_type=fb_exchange_token - The response will include a new
access_tokenfield—this is your permanent token. Store it securely (e.g., Airflow connections, environment variables, or a secrets manager) never hardcode it in your code.
3. Integrate with Your Airflow Plugin
Now that you have your account_id and permanent token:
- In Airflow, create a new connection for Facebook Ads. Put your
account_id(act_ format) in the "Extra" field (e.g.,{"account_id": "act_123456"}), and the permanent token in the "Password" field. - Update the
facebook_ads_hook.pyto pull these values from the Airflow connection. When constructing the API URI, it should look like this:uri = f"https://graph.facebook.com/v{api_version}/{account_id}/insights?access_token={access_token}&breakdowns={breakdowns}&fields={fields}" - Test the hook to ensure it can pull data without any user prompts—you're all set for automated downloads!
Key Notes
- Permissions Matter: Double-check that your system user has
ads_readpermission; without it, the API will return a permission denied error. - Token Health Checks: Add a simple validation step in your automation (e.g., call
GET /mewith the token) to ensure it's still valid. Tokens only become invalid if the system user's permissions are revoked or the Business Manager is deleted. - Security: Never commit tokens to version control. Use Airflow's secure connection storage or a dedicated secrets manager to keep them safe.
内容的提问来源于stack exchange,提问作者Luis
相关产品推荐
相关产品推荐

