【发布时间】:2019-04-22 18:38:23
【问题描述】:
在我的生产服务器上,我收到以下错误
"init() 得到了一个意外的关键字参数 'apikey'"
开发服务器上的相同代码正在运行。
我的生产服务器正在运行 gunicorn,并且我已将环境变量 SENDGRID_API_KEY 添加到 gunicorn.service 文件中。我已经重新启动了 gunicorn 和 nginx。可以看到环境变量已经加载完毕。
我调用发送电子邮件的方法如下:
def sendtestemail(to):
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("<myemail>@<mydomain>.com")
to_email = Email(to)
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
return [response.status_code, response.body, response.headers]
【问题讨论】:
-
你的
SENDGRID_API_KEY在哪里???我几乎已经知道答案了,但只是想在回答之前确定一下。 -
嗨,它在 gunicorn.service 文件中,我已将其设置为 "-e SENDGRID_API_KEY=
。我知道这可能并不理想,但只是试图让它如果我尝试引用一个文件,它就不会起作用。 -
嗯,我不确定该设置,但我的做法是将我的
SENDGRID_API_KEY添加到我的设置文件from projectroot.settings import SENDGRID_API_KEY,然后将该变量添加到您的@987654326 @ 目的。我在这里猜测 os.environ 没有按照您设置的方式获取变量。那个字典键肯定不在我的设置中。 -
感谢您的回复,我也尝试过,但不幸的是,我得到了同样的错误。
-
我发现开发服务器是5.6.0版本,生产服务器是6.0.4。我认为这与此有关。如果我将参数名称“apikey”更改为“api_key”,则会出现不同的错误。然后我得到的错误是“'电子邮件'类型的对象不是 JSON 可序列化的”,这表明它正在接受第一行代码。可能是这个函数已经过时了,但我已经检查了sendgrid.com/docs/for-developers/sending-email/… 的文档,它和我写的一样。我已联系 SendGrid 支持询问。
标签: python django sendgrid sendgrid-api-v3