簡體   English   中英

將Sendgrid集成到Google App Engine中

[英]Integrating Sendgrid into Google App Engine

我正在嘗試通過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')

有人可以幫忙這里發生的事情嗎?

謝謝。

我使用的語法與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。如您在第161行(包含_make_request該行的行)中包含_make_request 的client.py代碼中所看到的那樣 ,您正在獲取第159行中正在處理的HTTPError:

exc = handle_error(err)

handle_error() 在exceptions.py中實現,並顯示UnauthorizedError來自HTTPError401。最可能的原因是API密鑰錯誤。 檢查您的api鍵是否有錯字。 通過在我的代碼中插入錯誤的api鍵,我能夠獲得相同的HTTP錯誤。 除此之外,我可以使用您在問題中顯示的相同代碼來使用此功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM