python - Google Analytics API(Python 客户端库)- 错误处理

标签 python error-handling google-analytics google-analytics-api google-api-python-client

我正在 Google Analytics API(Python) 中使用批处理请求。批处理链接:https://developers.google.com/api-client-library/python/guide/batch 当通过 .add() 的所有记录都正确(有效)时,批处理工作正常。当一个或多个值无效时,所有记录的批处理都会失败。

我添加了一个回调函数来处理错误,并且我发现批处理中的所有记录的 BAtching 请求均失败(而不是仅针对无效记录)。 有没有办法处理错误并跳过无效的行/记录并继续处理批处理中的其余记录?

下面是我使用的示例代码和错误消息:

def add_user_callback(request_id, response, exception):
    if exception:
        print "error :",exception
    else:
        print "successful"

def main():
    ## code to set the account, property and other variables
    batch.add(service.management().webpropertyUserLinks().insert(
        accountId=account_id,
        webPropertyId=property_at,
        body={
                    'permissions': {
                        'local': [
                            'READ_AND_ANALYZE'
                        ]
                    },
                    'userRef': {
                        'email': 'valid_address@domain.com'
                    }
                }))

    batch.add(service.management().webpropertyUserLinks().insert(
        accountId=account_id,
        webPropertyId=property_at,
        body={
                    'permissions': {
                        'local': [
                            'READ_AND_ANALYZE'
                        ]
                    },
                    'userRef': {
                        'email': 'invalid_address@ddomain.com' ## i used a dummy id : pppp@domain.com
                    }
                }))
    batch.execute()


#Error :
#error : <HttpError 400 when requesting https://www.googleapis.com/analytics/v3/management/accounts/62974313/webproperties/UA-62974313-35/entityUserLinks?alt=json returned "Value for field user.email = ppppp@domain.com is not valid.">
#error : <HttpError 400 when requesting https://www.googleapis.com/analytics/v3/management/accounts/62974313/webproperties/UA-62974313-11/entityUserLinks?alt=json returned "Value for field user.email = ppppp@domain.com is not valid.">

如果您需要更多信息,请告诉我。

最佳答案

假设您有一个要添加到存储在 users 列表中的个人资料的用户列表。 您可以使用以下回调函数删除不良电子邮件:

def call_back(request_id, response, exception):
  if exception is not None:
    if isinstance(exception, HttpError):
      message = json.loads(exception.content)['error']['message']
      bad = 'Value for field user.email = (\S*) is not valid.'
      match = re.match(bad, message)
      if match:
        bad_user = match.group(1)
        if bad_user in users:
          users.remove(bad_user)
  else:
    print response

在所有失败的调用返回后,您可以通过循环用户并构造新的批量请求来重新尝试批量调用:

batch = BatchHttpRequest(callback=call_back)
for user in users:
    request = analytics.management().profileUserLinks().insert(
        accountId=ACCOUNT_ID,
        webPropertyId=PROFILE_ID,
        profileId=profile,
        body={
            'permissions': {'local': ['READ_AND_ANALYZE']},
            'userRef': {'email': user}
      }
    )
    batch.add(request, request_id=PROFILE_ID + user)
batch.execute()

关于python - Google Analytics API(Python 客户端库)- 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35609117/

相关文章:

google-analytics - Google Data Studio - 如何获取执行事件的用户百分比?

redirect - 如果可能的话,从 Google 中删除以下重定向链

python - 裁剪视频中的帧

python - 将 Excel 的列转换为 python 中的字典

python - 减小已保存视频的大小/分辨率

api - 如何通过Yii2 Api中的错误消息?

web-services - 在 RESTful 服务中使用 http 状态码

c# - Intrinio GetSecurityIntradayPrices 示例不起作用 - 将值 "iex"转换为类型 'System.Nullable' 时出错

android - Google Analytics 饼图小部件过滤器不起作用

python - Django:根据标识符将数据填充到模型中