【问题标题】:Integrating Sendgrid into Google App Engine将 Sendgrid 集成到 Google App Engine
【发布时间】:2017-07-17 14:51:12
【问题描述】:

我正在尝试使用 Sendgrid 发送密码重置电子邮件。

这是错误的堆栈跟踪:

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-client 都导入到我的项目中。 (为什么我必须单独导入这个?)

这是我从 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@example.com")
    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')

任何人都可以帮助解决这里发生的事情吗?

谢谢。

【问题讨论】:

    标签: python google-app-engine sendgrid sendgrid-api-v3


    【解决方案1】:

    我使用的语法不同,使用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)
    

    【讨论】:

    • 感谢 GAEfan,我应该明确表示我正在尝试集成 Web API v3。那是我也在使用的 SMTP 中继代码。集成到项目中要简单得多。
    【解决方案2】:

    检查您的 API 密钥是否有拼写错误,并确保使用 os.environ.get() 正确加载。

    您收到 HTTP 错误 401。正如您在第 161 行(以及通往它的行)上看到的包含 _make_requestin the client.py code,您将在第 159 行得到一个正在处理的 HTTPError:

    exc = handle_error(err)
    

    handle_error() 实现了in exceptions.py,并显示UnauthorizedError 来自HTTPError 401。最可能的原因是错误的api key。检查您的 api 密钥是否有拼写错误。通过在我的代码中插入一个错误的 api 密钥,我能够得到相同的 HTTP 错误。除此之外,我的此功能与您在问题中显示的代码相同。

    【讨论】:

    • 谢谢你,我检查了 api 密钥。
    • 尝试将 SENDGRID_API_KEY 放在引号中,如快速入门中所示:app.sendgrid.com/guide/integrate/langs/python。所以它看起来像sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
    猜你喜欢
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多