【发布时间】:2019-08-07 13:33:10
【问题描述】:
我正在开发一个基于 GAE(Google App Engine)的 python 应用程序,其中集成了 sendgrid python SDK(v3.2.10)。我现在正在尝试做的是,每当 sendgrid 推送类型为“bounce”的事件 webhook 时,我想从 sendgrid 上的退回电子邮件列表中删除该退回的电子邮件。
我已经浏览了官方网站上提供的文档。首先,我尝试使用 SDK 删除电子邮件地址,它在 localhost 上运行良好。但是在将其部署到实时服务器后,它什么也不做,属于例外条款。
代码sn-p:
try:
send_grid_client = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
data = {"emails": [email.strip()]}
delete_response = send_grid_client.client.suppression.bounces.delete(
request_body=data)
except Exception as exception:
logging.info('Exception is: {}'.format(exception))
pass
由于它没有按预期工作,我现在尝试使用 REST API 来做同样的事情。
代码sn-p:
import requests
data = {"emails": [email]}
headers = {"Authorization": "Bearer {}".format(SENDGRID_API_KEY)}
delete_response = requests.delete("https://api.sendgrid.com/v3/suppression/bounces", data=json.dumps(data), headers=headers)
logging.info(delete_response)
logging.info(delete_response.status_code)
logging.info(delete_response.text)
现在,sendgrid API 不断返回错误 400,并带有消息 {"errors":[{"field":null,"message":"emails or delete_all params required"}]}。我根本不知道如何克服这个问题。也许我错过了如何在 delete 函数中传递请求正文,但是我想不通。
【问题讨论】:
标签: python python-requests sendgrid sendgrid-api-v3 sendgrid-api-v2