1
我在Google Analytics API(Python)中使用批處理請求。鏈接到批處理:https://developers.google.com/api-client-library/python/guide/batch 當所有通過.add()的記錄正確(有效)時,批處理工作正常。當一個或多個值無效時,所有記錄的批處理失敗。Google AnalyticsAPI(用於Python的客戶端庫) - 錯誤處理
我添加了一個回調函數來處理錯誤,並且我看到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': '[email protected]'
}
}))
batch.add(service.management().webpropertyUserLinks().insert(
accountId=account_id,
webPropertyId=property_at,
body={
'permissions': {
'local': [
'READ_AND_ANALYZE'
]
},
'userRef': {
'email': '[email protected]' ## i used a dummy id : [email protected]
}
}))
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 = [email protected] 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 = [email protected] is not valid.">
請讓我知道如果你需要更多的信息。
在add方法中使用'request_id'來跟蹤失敗的請求,並只重新嘗試那些格式良好的請求。 整個批處理請求需要是原子的,因爲它可能需要多個請求來更改單個用戶的ACL - 想象將用戶從帳戶級別移動到視圖級別所需的請求。 – Matt
Matt,你可以指點一些示例代碼來處理request_id。當出現異常時,我試圖在回調函數中處理它。這裏是我使用的代碼:'bad_email = re.search(r'[a-zA-Z0-9] * @ [az] * [。] [az] * \ S',str(例外)) bad_emails .append(bad_email) 打印「bad_emails列表中的郵件:」,bad_emails [0] create_batch(email_list,bad_emails)'create_batch()在這裏調用函數來創建批處理(withtout bad_emails) –
我已經添加了一個更完整以下解決方案可幫助您從響應中提取有問題的電子郵件。 – Matt