将Sendgrid集成到Google App Engine中
我正尝试使用Sendgrid发送密码重置电子邮件。将Sendgrid集成到Google App Engine中
以下是错误的堆栈跟踪:
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\lib\webapp2-
2.5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files\Google\google_appengine\lib\webapp2-
2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files\Google\google_appengine\lib\webapp2-
2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files\Google\google_appengine\lib\webapp2-
2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files\Google\google_appengine\lib\webapp2-
2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files\Google\google_appengine\lib\webapp2-
2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\C\PycharmProjects\alpha\u\user.py", line 121, in post
response = sg.client.mail.send.post(request_body=mail.get())
File "C:\Users\C\PycharmProjects\alpha\python_http_client\client.py",
line 227, in http_request
return Response(self._make_request(opener, request))
File "C:\Users\C\PycharmProjects\alpha\python_http_client\client.py",
line 161, in _make_request
raise exc
UnauthorizedError
我已经进口都sendgrid和python-HTTP客户端在我的项目。 (我为什么要导入这个分开?)
这里从Sendgrid演示了我的测试代码:
class PasswordResetHandler(BaseHandler):
"""
handler to reset the users password.
also to verify if the user email is in the database
"""
def post(self):
email = self.request.get("email_for_reset")
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get(SENDGRID_API_KEY))
from_email = Email("[email protected]")
to_email = Email(email)
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())
self.redirect('/u/password-reset-confirmation')
任何人都可以对什么是怎么回事帮助吗?
谢谢。
我使用的语法是不同的,使用SendGridClient
:
sg = sendgrid.SendGridClient(SENDGRID_API_KEY)
message = sendgrid.Mail(
to = to,
subject = subject,
html = html,
text = body,
from_email = sender
)
status, msg = sg.send(message)
检查拼写错误您的API密钥,并确保它与os.environ.get()
正确加载。
你得到HTTP错误401,你可以看到一个包含in the client.py code_make_request
上线161(并导致它的行),您获得的是正在线159处理引发HTTPError:
exc = handle_error(err)
handle_error()
实施in exceptions.py,并且显示UnauthorizedError
来自HTTPError 401.最可能的原因是api密钥不好。检查您的API密钥是否有错别字。我可以通过在我的代码中插入一个错误的api密钥来获得相同的HTTP错误。除此之外,我有这个功能与您在问题中显示的相同代码一起工作。
谢谢你,我检查了API密钥。 – zmdc
尝试将SENDGRID_API_KEY放在引号中,如快速入门所示:https://app.sendgrid.com/guide/integrate/langs/python。所以它看起来像'sg = sendgrid.SendGridAPIClient(apikey = os.environ.get('SENDGRID_API_KEY'))' –
感谢GAEfan,我应该明确表示我正在尝试集成Web API v3。这是SMTP中继代码,我也一样。融入项目要简单得多。 – zmdc