facebook / facebook-python-business-sdk

Python SDK for Meta Marketing APIs
https://developers.facebook.com/docs/business-sdk
Other
1.27k stars 629 forks source link

facebook_business.adobjects.business.Business get_ad_network_analytics() ERROR #661

Closed WendyE closed 5 months ago

WendyE commented 5 months ago

facebook_business.adobjects.business.Business get_ad_network_analytics() ERROR

I‘m get data from https://graph.facebook.com/v18.0/{app_id}/adnetworkanalytics with params:

{'metrics': '["FB_AD_NETWORK_REVENUE"]', 'breakdowns': '["APP","COUNTRY","PLACEMENT"]', 'aggregation_period': 'DAY', 'since': '2024-01-16', 'until': '2024-01-17', 'limit': 2000}

then,I get the first page response paging like this:

{'cursors': {'before': 'MTM3OAZDZD', 'after': 'MTM3NwZDZD'}, 'next': '{next_url}'} 

and get the last page response paging like this:

{'cursors': {'before': 'MTM3OAZDZD', 'after': 'MTM3NwZDZD'}, 'previous': '{previous_url}'} 

BUT, Business.get_ad_network_analytics() will infinite loop to request the last page!!! Finally,I got response:

Status:  400
  Response:
    {
      "error": {
        "message": "(#613) You can have at most 250 queries per minute",
        "type": "OAuthException",
        "code": 613,
        "fbtrace_id": "AXh2zHUPkO8EALtDlsgV1RR"
      }
    }

I debug the facebook_business code,find the code in facebook_business.api load_next_page() line 835:

if (
            'paging' in response and
            'cursors' in response['paging'] and
            'after' in response['paging']['cursors']
        ):
            self.params['after'] = response['paging']['cursors']['after']
else:
    # Indicate if this was the last page
    self._finished_iteration = True

if condition always stand up!because response['paging']['cursors']['after'] always true!

Now, what can I do to resolve this problem?

stcheng commented 5 months ago

thanks for reporting this bug. we will provide a fix in our next release to instead check the existence of 'next' in response['paging'] to decide if it is the last page.

stcheng commented 5 months ago

https://github.com/facebook/facebook-python-business-sdk/commit/21557b2a47f421c2112b9564029d51fdad517620

WendyE commented 5 months ago

OK!thanks!