pythonfacebook-graph-apifacebook-business-sdkmatillion

(#2635) You are calling a deprecated version of the Ads API. Please update to the latest version


so there have been a bunch of similar questions, though I haven't managed to find the answer in any of them, so here we are. I'm trying to pull some data from the Facebook API using Python, and at here's the error I'm getting:

facebook_business.exceptions.FacebookRequestError: 

  Message: Call was not successful
  Method:  GET
  Path:    https://graph.facebook.com/v10.0/[hidden]/ads
  Params:  {'summary': 'true'}

  Status:  400
  Response:
    {
      "error": {
        "message": "(#2635) You are calling a deprecated version of the Ads API. Please update to the latest version: v11.0.",
        "type": "OAuthException",
        "code": 2635,
        "fbtrace_id": "[hidden]"
      }
    }

I headed right to the app settings in my Facebook profile, which looked like (pic related)enter image description here

Please note that in the error message there is v10.0 in the path, and before any changes API version in the app profile was v7.0, which is quite odd, is there some other API that needs its version switched as well? Switching that one to v12.0 (or v11.0 at that) hasn't helped, so I really don't know what to do anymore.

Another important note is that this code is a part of a scheduled job in Matillion, and after the version switch the code does actually return data if run locally, so another hunch of mine is that some changes need to be implemented to Matillion environment as well.

Here's what the code looks like:

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adreportrun import AdReportRun
import time
from io import StringIO
import json
import boto3

access_token = [hidden]
app_secret = [hidden]
app_id = [hidden]


FacebookAdsApi.init(app_id, app_secret, access_token)

fb_account = AdAccount('[hidden]')

params = {'time_range': {'since': '2022-01-05', 'until': '2022-01-06'},
            'level':'ad',
            'action_breakdowns':['action_device','action_type'],
            'TimeIncrement':1,
             }
fields = [<a pretty big list of fields>]

ads=fb_account.get_ads()
async_job = fb_account.get_insights(params = params, fields = fields,is_async=True)
async_job.api_get()

while async_job[AdReportRun.Field.async_status] != 'Job Completed' or async_job[AdReportRun.Field.async_percent_completion] < 100:
        time.sleep(1)
        async_job.api_get()
time.sleep(1)
responce=async_job.get_result()


print(response)

Please feel free to ask any questions if I forgot to mention some details, thank you!


Solution

  • The Facebook Python modules you are using do not get installed by default with Matillion. I guess someone must have installed them by following this Matillion guide? Maybe it was done a long time ago, in which case you might just need to re-install the modules with more up-to-date versions that talk to the newer Facebook API.

    As an alternative to Python, you could also take a look at the Facebook Ads Query component which is a top-level Matillion orchestration component that does not require any coding in Python. According to the docs it uses V11 of the API. Provided the data you need is in that component's data model, it could be less work overall than handcoding.