I am getting an error when using the facebook api

限于喜欢 提交于 2021-01-27 20:45:44

问题


 File "SAMPLE_CODE.py", line 21, in <module>
    from facebookads.adobjects.adaccount import AdAccount
  File "/usr/local/lib/python3.7/site-packages/facebookads/adobjects/adaccount.py", line 1582
    def get_insights(self, fields=None, params=None, async=False, batch=None, pending=False):
                                                         ^
SyntaxError: invalid syntax

I am trying to use the Facebook API and I am getting this error. Not sure what to do about it. This is my first time setting up and trying to use this API. Any help would be appreciated

Here is the file I am trying to run

"""python

# Copyright 2014 Facebook, Inc.

# You are hereby granted a non-exclusive, worldwide, royalty-free license to
# use, copy, modify, and distribute this software in source code or binary
# form for use in connection with the web services and APIs provided by
# Facebook.

# As with any software that integrates with the Facebook platform, your use
# of this software is subject to the Facebook Developer Principles and
# Policies [http://developers.facebook.com/policy/]. This copyright notice
# shall be included in all copies or substantial portions of the software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from facebookads.adobjects.adaccount import AdAccount
from facebookads.adobjects.adsinsights import AdsInsights
from facebookads.api import FacebookAdsApi

access_token = 'token'
ad_account_id = 'id'
app_secret = 'secret'
app_id = 'id'
FacebookAdsApi.init(access_token=access_token)

fields = [
    'reach',
    'delivery',
    'cost_per_result',
    'cpp',
    'cpm',
    'cost_per_action_type:page_engagement',
    'purchase_roas:omni_purchase',
    'website_purchase_roas:offsite_conversion_fb_pixel_purchase',
    'mobile_app_purchase_roas:app_custom_event_fb_mobile_purchase',
    'campaign_group_name',
]
params = {
    'level': 'ad',
    'filtering': [{'field':'delivery_info','operator':'IN','value':['active']}],
    'breakdowns': ['days_1'],
    'time_range': {'since':'2019-11-14','until':'2019-11-21'},
}
print(AdAccount(ad_account_id).get_insights(
    fields=fields,
    params=params,
))

"""

Here is another stack overflow question where async= False is the error too

Problem with connect facebookads library for extract data from Facebook with Marketing API using Python

Could someone explain what is going on here ?

Thanks

I downloaded version 3.6 of Python using pyenv and Homebrew and tried to run the file again but I got a new error code.

"""

Kiefer's Macbook Pro:facebook-python-business-sdk kiefergallant$ python3.6 SAMPLE_CODE.py 
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: value of breakdowns might not be compatible.  Expect list<breakdowns_enum>;  got <class 'list'>
  warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field delivery
  warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field cost_per_result
  warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field cost_per_action_type:page_engagement
  warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field purchase_roas:omni_purchase
  warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field website_purchase_roas:offsite_conversion_fb_pixel_purchase
  warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field mobile_app_purchase_roas:app_custom_event_fb_mobile_purchase
  warnings.warn(message)
/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/utils/api_utils.py:30: UserWarning: insights does not allow field campaign_group_name
  warnings.warn(message)
Traceback (most recent call last):
  File "SAMPLE_CODE.py", line 51, in <module>
    params=params
  File "/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/adobjects/adaccount.py", line 1639, in get_insights
    return request.execute()
  File "/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/api.py", line 664, in execute
    cursor.load_next_page()
  File "/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/api.py", line 851, in load_next_page
    params=self.params,
  File "/Users/kiefergallant/.pyenv/versions/3.6.0/lib/python3.6/site-packages/facebookads/api.py", line 332, in call
    raise fb_response.error()
facebookads.exceptions.FacebookRequestError: 

  Message: Call was not successful
  Method:  GET
  Path:    https://graph.facebook.com/v2.11/act_423073558387349/insights
  Params:  {'level': 'ad', 'filtering': '[{"field":"delivery_info","operator":"IN","value":["active"]}]', 'breakdowns': '["days_1"]', 'time_range': '{"since":"2019-11-14","until":"2019-11-21"}', 'fields': 'reach,delivery,cost_per_result,cpp,cpm,cost_per_action_type:page_engagement,purchase_roas:omni_purchase,website_purchase_roas:offsite_conversion_fb_pixel_purchase,mobile_app_purchase_roas:app_custom_event_fb_mobile_purchase,campaign_group_name'}

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

"""


回答1:


Taken from this Question:

The version of your API and Python are not compatible.

The error is caused by the parameter called async from get_insights(), which since Python 3.7 isn't allowed anymore since async is now a reserved keyword of python.

Solution: Use an older Python version (<3.7) or use a version of the API that is compatible with Python 3.7.




回答2:


Try facebook_business instead of facebookads. See this.



来源:https://stackoverflow.com/questions/58953071/i-am-getting-an-error-when-using-the-facebook-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!