2017-07-17 84 views
1

我正嘗試使用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') 

任何人都可以對什麼是怎麼回事幫助嗎?

謝謝。

回答

0

我使用的語法是不同的,使用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) 
+0

感謝GAEfan,我應該明確表示我正在嘗試集成Web API v3。這是SMTP中繼代碼,我也一樣。融入項目要簡單得多。 – zmdc

0

檢查拼寫錯誤您的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錯誤。除此之外,我有這個功能與您在問題中顯示的相同代碼一起工作。

+0

謝謝你,我檢查了API密鑰。 – zmdc

+0

嘗試將SENDGRID_API_KEY放在引號中,如快速入門所示:https://app.sendgrid.com/guide/integrate/langs/python。所以它看起來像'sg = sendgrid.SendGridAPIClient(apikey = os.environ.get('SENDGRID_API_KEY'))' –