我在使用 Facebook 时遇到错误 api

I am getting an error when using the facebook api

 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

我正在尝试使用 Facebook API,但出现此错误。不知道该怎么办。这是我第一次设置和尝试使用这个 API。任何帮助将不胜感激

这是我正在尝试的文件 运行

"""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,
))

"""

这是另一个堆栈溢出问题,其中 async= False 也是错误

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

有人能解释一下这是怎么回事吗?

谢谢

我使用 pyenv 和 Homebrew 下载了 Python 的 3.6 版,并尝试再次 运行 该文件,但我得到了一个新的错误代码。

"""

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"
      }
    }

"""

摘自this Question:

您的 APIPython 的版本不兼容。

错误是由 get_insights() 中名为 async 的参数引起的,因为 Python 3.7 不再被允许,因为 async 现在是python.

解决方案:使用旧的Python版本(<3.7)或使用[=35=的版本] 那是 兼容 Python 3.7.

尝试 facebook_business 而不是 facebookads。参见 this