使用 facebook api 时出现错误

Posted

技术标签:

【中文标题】使用 facebook api 时出现错误【英文标题】:I am getting an error when using the facebook api 【发布时间】:2019-11-20 11:16:34 【问题描述】:
 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。任何帮助将不胜感激

这是我要运行的文件

"""蟒蛇

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

"""

【问题讨论】:

嗨,你能告诉我们你的源代码吗?否则,我们帮不了你太多。 在哪里可以找到源代码?你想要有错误的文件吗?这是我从 facebook 下载的文件 好的,我刚刚用源代码编辑了帖子 “这是我要运行的文件” - 这似乎不是错误消息所指的文件:“文件/usr/local/lib/python3.7/site-packages/facebookads/adobjects/adaccount.py ,第 1582 行" 您可能希望从源代码中删除您的令牌和 ID。编辑:在线共享时,而不是在您的实际源代码中。 【参考方案1】:

取自this Question:

您的 APIPython 版本不兼容。

该错误是由来自get_insights() 的名为async 的参数引起的,由于async 现在是python 的保留关键字,因此不再允许使用Python 3.7。

解决方案:使用较旧的 Python 版本 () 或使用符合以下条件的 API 版本 与 Python 3.7 兼容。

【讨论】:

我应该如何获得旧版本的 python ?我刚刚尝试使用 homebrew 和 pyenv 下载 python 3.6,我运行了 SAMPLE_CODE.py,它给了我一堆新错误【参考方案2】:

尝试facebook_business 而不是facebookads。见this。

【讨论】:

以上是关于使用 facebook api 时出现错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 facebook API 的 stream.publish 方法时出现权限错误

通过 Graph API 和 iOS 将照片上传到相册时出现 Facebook 错误:(#1) 发生未知错误

尝试实现自定义字体时出现“@font-face 声明不遵循 fontspring 防弹语法”错误

尝试使用 Parse PFFacebookUtils 登录 Facebook 时出现错误 307

尝试使用 smack 连接 facebook 聊天时出现此错误

登录 Facebook 时出现 com.facebook.sdk 错误 2