首页 > 解决方案 > Facebook 广告 API 对少数帐户失败,而不是对所有帐户

问题描述

我不确定 Facebook 广告 API 发生了什么,但它开始向我抛出以下错误。昨天一切都很好。此错误仅适用于以下live_accounts第一个帐户的少数帐户,它不会给我一个错误,但第二个帐户会抛出一个错误。

错误:

raise fb_response.error()
facebook_business.exceptions.FacebookRequestError: 

  Message: Call was not successful
  Method:  GET
  Path:    https://graph.facebook.com/v9.0/act_25XX93XXX763XXX/insights
  Params:  {'time_range': '{"since":"2021-04-24","until":"2021-04-24"}', 'breakdowns': '["publisher_platform","platform_position"]', 'action_breakdowns': '["action_type"]', 'level': 'ad', 'time_increment': 1, 'limit': 1, 'fields': 'adset_name,ad_name,campaign_name,account_name,impressions,account_currency,video_p25_watched_actions,video_p50_watched_actions,video_p75_watched_actions,video_p100_watched_actions,inline_link_clicks,spend,actions,action_values'}

  Status:  500
  Response:
    {
      "error": {
        "code": 1,
        "message": "Please reduce the amount of data you're asking for, then retry your request"
      }
    }

这是我的代码

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adsinsights import AdsInsights
from facebook_business.adobjects.adaccount import AdAccount
import pandas as pd
from facebook_business.adobjects.user import User
from datetime import date
from datetime import timedelta
from google.cloud import storage
import os

start = '2021-04-24'
end = '2021-04-24'
FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token, api_version='v9.0')
me = User(fbid="me")
my_account = me.get_ad_accounts()
account_list = pd.DataFrame(my_account)
appended_data = []
live_accounts = ['21XXX478XXX279X', '25XX93XXX763XXX']
for i in live_accounts:
    print(i)
    act = AdAccount('act_{}'.format(i))
    async_job = act.get_insights(params={'time_range': {'since': start, 'until': end},
                                         'breakdowns': ['publisher_platform', 'platform_position'],
                                         'action_breakdowns': ['action_type'], 'level': 'ad', 'time_increment': 1,
                                         'limit': 1,
                                         },
                                 # is_async=True,
                                 fields=[AdsInsights.Field.adset_name,
                                         AdsInsights.Field.ad_name,
                                         AdsInsights.Field.campaign_name,
                                         AdsInsights.Field.account_name,
                                         AdsInsights.Field.impressions,
                                         AdsInsights.Field.account_currency,
                                         AdsInsights.Field.video_p25_watched_actions,
                                         AdsInsights.Field.video_p50_watched_actions,
                                         AdsInsights.Field.video_p75_watched_actions,
                                         AdsInsights.Field.video_p100_watched_actions,
                                         AdsInsights.Field.inline_link_clicks,
                                         AdsInsights.Field.spend,
                                         AdsInsights.Field.actions,
                                         AdsInsights.Field.action_values,
                                         ])
                                         
    results = []
    for item in async_job:
        print(item, type(item), async_job)
        data = dict(item)
        results.append(data)
    

我尝试过传入is_async=Trueget_insights方法,但作为回报,它只给了我 8 行似乎不正确的信息。

请帮忙。

标签: pythonfacebook-ads-api

解决方案


尝试先进入广告级别,然后访问 Ad(< ad_id >).get_insights()。

当您尝试从 AdAccount 级别获取 'level':'ad' - 包含许多字段时,它会引发此错误。

示例代码:

res = []
for i in live_accounts:
    print(i)
    act = AdAccount('act_{}'.format(i))

    ads = act.get_ads(params={'time_range': {'since': start, 'until': end}})
    for ad in ads:

        ad_ins = ad.get_insights(params={'time_range': {'since': start, 'until': end},
                                         'breakdowns': ['publisher_platform', 'platform_position'],
                                         'action_breakdowns': ['action_type'], 
                                         'time_increment': 1,
                                         'limit': 500
                                         },

                                 fields=[AdsInsights.Field.adset_name,
                                         AdsInsights.Field.ad_name,
                                         AdsInsights.Field.campaign_name,
                                         AdsInsights.Field.account_name,
                                         AdsInsights.Field.impressions,
                                         AdsInsights.Field.account_currency,
                                         AdsInsights.Field.video_p25_watched_actions,
                                         AdsInsights.Field.video_p50_watched_actions,
                                         AdsInsights.Field.video_p75_watched_actions,
                                         AdsInsights.Field.video_p100_watched_actions,
                                         AdsInsights.Field.inline_link_clicks,
                                         AdsInsights.Field.spend,
                                         AdsInsights.Field.actions,
                                         AdsInsights.Field.action_values,
                                         ])
        res.append(ad_ins)

另一种方法是在这个例子中使用异步:https ://github.com/facebook/facebook-python-business-sdk/blob/master/examples/async.py


推荐阅读