我通过典型方法在 Gmail 的网络界面中创建了一个标签“handler_gmail”。
当我尝试通过 Gmail API(Python 客户端库)为邮件设置该标签时,我收到响应 HttpError 400,返回“无效标签:handler_gmail”。
我确定该标签存在(我检查了多次 – 没有拼写错误)。
我试着这样设置:
gs.users().messages().modify(userId=用户名, id=m['id'], body={"addLabelIds": ['handler_gmail']}).execute()
我尝试将 "removeLabelIds": []
键/值对添加到字典中(认为这可能是必需的),但出现了相同的“无效标签”错误。
非常感谢任何帮助!非常接近能够完成我需要完成的项目!
最佳答案
正如其他人所指出的,您必须使用标签 ID,而不是标签名称。我通过使用 gmail API 的 sample code 解决了这个问题并修改它以打印出除名称之外的 ID:
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
if not labels:
print('No labels found.')
else:
print('Labels:')
for label in labels:
print(label['name']+ " "+label['id'])
所以一旦你得到相应的id,你的代码应该是这样的:
gs.users().messages().modify(userId=username, id=m['id'], body={"addLabelIds":
['Label_1']}).execute()
关于python - Gmail API : "Invalid Label" When Trying to Set Message Label to One That I Created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26963868/