facebook-python-business-sdk icon indicating copy to clipboard operation
facebook-python-business-sdk copied to clipboard

AttributeError when using sessions for insights

Open s-nez opened this issue 9 years ago • 1 comments

The code (user credentials removed):

from facebookads import FacebookAdsApi, FacebookSession
from facebookads.adobjects.adaccountuser import AdAccountUser

app_id, app_secret, access_token = ...

session = FacebookSession(app_id, app_secret, access_token)
api     = FacebookAdsApi(session)
user    = AdAccountUser(fbid = 'me', api = api)
account = user.get_ad_account()
report_run = account.get_insights_async()
report_run.remote_read()

This fails with:

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    report_run.remote_read()
  File ".../lib/python2.6/site-packages/facebookads/adobjects/abstractcrudobject.py", line 358, in remote_read
    self = request.execute()
  File ".../lib/python2.6/site-packages/facebookads/api.py", line 665, in execute
    return response
  File "/usr/lib64/python2.6/contextlib.py", line 34, in __exit__
    self.gen.throw(type, value, traceback)
  File ".../lib/python2.6/site-packages/facebookads/api.py", line 825, in open_files
    yield opened_files
  File ".../lib/python2.6/site-packages/facebookads/api.py", line 653, in execute
    response = self._api.call(
AttributeError: 'NoneType' object has no attribute 'call'

It works when a default api is present, but in a session it fails. The problem seems to be in adobjects/adaccount.py:

def get_insights_async(self, fields=None, params=None, batch=None, pending=False):
    ...
    request = FacebookRequest(
        node_id=self['id'],
        method='POST',
        endpoint='/insights',
        api=self._api,
        param_checker=TypeChecker(param_types, enums),
        target_class=AdReportRun,
        api_type='EDGE',
        response_parser=ObjectParser(target_class=AdReportRun),
        include_summary=False,
    )
    ...

When ObjectParser is instantiated, it doesn't get an api argument, resulting in the AdReportRun object not having one as well and failing to call it since there is no default.

If you replace the response_parser argument with:

response_parser=ObjectParser(target_class=AdReportRun, api = self._api),

the code works. I'm not sure if that's the correct solution, though, since nowhere else in the code ObjectParser gets an api argument.

s-nez avatar Apr 26 '17 14:04 s-nez

Thank s-nez for your feedback! The issue has been fixed and it will become available in next push :)

ISUFB avatar Jun 22 '17 22:06 ISUFB