使用Google AdWords API的ManagedCustomerService时遇OPERATOR_NOT_SUPPORTED错误
解决ManagedCustomerService mutateLabel()的OPERATOR_NOT_SUPPORTED错误
嘿,你这是踩了AdWords API的一个典型小坑——ManagedCustomerService的mutateLabel方法根本不支持SET操作符!这个API的设计逻辑就是用来管理客户账号和标签之间的关联关系,只允许用ADD(给账号加标签)和REMOVE(给账号删标签)这两种操作,压根没提供直接“更新”标签的SET选项,所以你用SET肯定会触发OPERATOR_NOT_SUPPORTED错误。
下面分两种常见场景给你解决方案:
场景1:你想替换账号的某个标签(旧标签换新标签)
如果你的“更新标签”是指把账号上的某个旧标签换成新标签,那得拆成两步走:先移除旧标签,再添加新标签。修改后的代码示例如下(顺便帮你修正了原代码里的拼写错误accrued→occurred):
from googleads import adwords COSTOMER_ID = 'xxxxxxxxxx' OLD_LABEL_ID = '你的旧标签ID' NEW_LABEL_ID = '你的新标签ID' adwords_client = adwords.AdWordsClient.LoadFromStorage('googleads.yaml') manage_customer_service = adwords_client.GetService('ManagedCustomerService', version='v201809') # 先移除旧标签,再添加新标签 operations = [ # 移除旧标签 { "operator": "REMOVE", "operand": { "labelId": OLD_LABEL_ID, "customerId": COSTOMER_ID } }, # 添加新标签 { "operator": "ADD", "operand": { "labelId": NEW_LABEL_ID, "customerId": COSTOMER_ID } } ] try: response = manage_customer_service.mutateLabel(operations) print("标签替换成功:", response) except Exception as e: print('Exception occurred:') print(e)
场景2:你想修改标签本身的属性(比如名称、颜色)
如果你的“更新标签”是指修改标签自己的信息(比如把标签名从"旧名称"改成"新名称"),那你找错API了!ManagedCustomerService只负责管账号和标签的关联,要改标签本身的属性,得用LabelService的mutate方法,示例代码如下:
from googleads import adwords LABEL_ID = 'xxxxxxxxxx' NEW_LABEL_NAME = '我的新标签名' adwords_client = adwords.AdWordsClient.LoadFromStorage('googleads.yaml') label_service = adwords_client.GetService('LabelService', version='v201809') operations = [ { "operator": "SET", "operand": { "id": LABEL_ID, "name": NEW_LABEL_NAME, # 如果需要改颜色也可以加这行:"color": "#FF0000" } } ] try: response = label_service.mutate(operations) print("标签属性更新成功:", response) except Exception as e: print('Exception occurred:') print(e)
最后提个小建议:你用的v201809版本AdWords API已经停止官方支持好久了,建议尽快升级到最新的Google Ads API,避免后续遇到更多兼容性问题。
内容的提问来源于stack exchange,提问作者Ashok Mamnanni
相关产品推荐
相关产品推荐

