2017-07-17 15 views
1

Sendgridでパスワードリセットのメールを送信しようとしています。ここで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クライアントの両方をインポートしました。ここで(?なぜ私は別にこれをインポートする必要があります)

は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を使用して、私が使用する構文が異なる

答えて

0

、:

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()で正しくロードされていることを確認します。あなたはin the client.py codeは、ライン161(およびそれに至るまでのライン)上_make_requestを含む見ることができるようにあなたがHTTPエラー401を取得している

、あなたはライン159で処理されているHTTPErrorを得ている:

exc = handle_error(err) 

handle_error()in exceptions.pyと実装されており、UnauthorizedErrorがHTTPError 401から取得されたことを示しています。最も可能性の高い原因は不正なapiキーです。 apiキーで誤字がないか確認してください。私は自分のコードに不正なapiキーを挿入することで同じHTTPエラーを取得することができました。それ以外にも、私はこの機能を、あなたが質問で示したのと同じコードで動作させています。

+0

ありがとう、私はAPIキーをチェックします。 – zmdc

+0

クイックスタート:https://app.sendgrid.com/guide/integrate/langs/pythonに示すように、SENDGRID_API_KEYを引用符で囲んでみてください。したがって、 'sg = sendgrid.SendGridAPIClient(apikey = os.environ.get( 'SENDGRID_API_KEY'))'のようになります。 –

関連する問題