使用Python3访问Google的API,特别是Tag Manager v2;我想批量删除特定的触发器。我收到400错误请求:INVALID_ARGUMENT。我需要帮助来了解传递给API命令的值的无效之处。
使用Pythn3.7,使用PyCharm CE,我通过Chrome(版本76.0.3809.132(正式版本)(64位))成功使用OAuth 2.0进行连接,我允许GTM访问,并且Google通过身份验证成功,成为了标签管理者。 dat文件已创建。目的是基于正则表达式模式批量删除触发器。
注意:我使用的是基本的Console Developers帐户。我了解配额限制,我的代码解释了这些限制。
我所做的:
getContainerPath = ''
getTriggerName = ''
getAMSIAccount = service.accounts().list().execute()['account'][0]['path']
getAMSIContainers = service.accounts().containers().list(parent=getAMSIAccount).execute()['container']
gtmTriggerErrorHandling = open('gtmTriggerErrorHandling.txt','w')
queries = 2
for container in getAMSIContainers:
queries += 1
print(str(queries) + ' ###### ' + container['name'])
if queries >= 24:
time.sleep(101)
queries = 1
elif queries < 24:
print(str(queries) + ' | ' + container['name'] + ' | ' + container['containerId'] + ' | ' + container['publicId'])
getContainerPath = container['path']
getAMSIWorkspace = service.accounts().containers().workspaces().list(parent=getContainerPath).execute()['workspace'][0]
getAMSIWorkspacePath = getAMSIWorkspace['path']
getAMSITriggers = service.accounts().containers().workspaces().triggers().list(parent=getAMSIWorkspacePath).execute()['trigger']
queries += 2
for trigger in getAMSITriggers:
print(str(queries) + ': ' + trigger['name'])
getTriggerName = re.search(r'Event - [Dd][Oo] - ([Ss][Rr][Pp]|[Vv][Dd][Pp]) - ([Nn][Ee][Ww]|[Uu][Ss][Ee][Dd]).*', trigger['name'])
getTriggerIgnore = re.search(r'^Event - DO - (SRP|VDP) - (New|Used) - Regex Table$', trigger['name'])
if not getTriggerName:
continue
elif queries < 24 and getTriggerName and not getTriggerIgnore:
getTriggerPath = trigger['path']
print(getTriggerPath)
try:
service.accounts().containers().workspaces().triggers().delete(path=getTriggerPath).execute()
except HttpError as error:
gtmTriggerErrorHandling.write('There was an API error : %s : %s : %s : %s : %s' %(error.resp.status, error.resp.reason, error.uri, error.error_details, error.content) + '\n')
queries += 1
elif queries >= 24:
time.sleep(101)
queries = 1
else:
continue
以下是错误输出:
There was an API error : 400 : Bad Request : https://www.googleapis.com/tagmanager/v2/accounts/256581/containers/460206/workspaces/494/triggers/446? : : b'{\n "error": {\n "code": 400,\n "message": "Returned an error response for your request.",\n "errors": [\n {\n "message": "Returned an error response for your request.",\n "domain": "global",\n "reason": "badRequest"\n }\n ],\n "status": "INVALID_ARGUMENT"\n }\n}\n'
There was an API error : 400 : Bad Request : https://www.googleapis.com/tagmanager/v2/accounts/256581/containers/460206/workspaces/494/triggers/447? : : b'{\n "error": {\n "code": 400,\n "message": "Returned an error response for your request.",\n "errors": [\n {\n "message": "Returned an error response for your request.",\n "domain": "global",\n "reason": "badRequest"\n }\n ],\n "status": "INVALID_ARGUMENT"\n }\n}\n'
There was an API error : 400 : Bad Request : https://www.googleapis.com/tagmanager/v2/accounts/256581/containers/460206/workspaces/494/triggers/448? : : b'{\n "error": {\n "code": 400,\n "message": "Returned an error response for your request.",\n "errors": [\n {\n "message": "Returned an error response for your request.",\n "domain": "global",\n "reason": "badRequest"\n }\n ],\n "status": "INVALID_ARGUMENT"\n }\n}\n'
There was an API error : 400 : Bad Request : https://www.googleapis.com/tagmanager/v2/accounts/256581/containers/460206/workspaces/494/triggers/449? : : b'{\n "error": {\n "code": 400,\n "message": "Returned an error response for your request.",\n "errors": [\n {\n "message": "Returned an error response for your request.",\n "domain": "global",\n "reason": "badRequest"\n }\n ],\n "status": "INVALID_ARGUMENT"\n }\n}\n'
这四个图像表明这些触发器存在于容器中。请参阅每张图片底部的状态消息,它将与上面的每个错误匹配。
https://i.stack.imgur.com/pujdj.png
https://i.stack.imgur.com/MaJaU.png
https://i.stack.imgur.com/fuCU7.png
https://i.stack.imgur.com/bN8wh.png
期望是基于正则表达式,使用GTM GUI时,这些触发器将置于“概述”中的“工作区更改”下。
谢谢您的指导。
答案 0 :(得分:0)
答案很简单,就在我眼前。我尝试删除的触发器仍与标签相关联。这是错误消息的原因。该API从逻辑上正确地不会删除触发器,只要它与标签相关联即可。