我已将 sendgrid 添加到我的 django 应用程序中 从这里开始执行简单的步骤https://github.com/elbuo8/sendgrid-django
生成帐户并复制 sengrid 站点上的 api
已将代码添加到我的 View
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e0838f8d8d85928385a0878d81898cce838f8d" rel="noreferrer noopener nofollow">[email protected]</a>")
to_email = Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cf0f3ecfddcfbf1fdf5f0b2fff3f1" rel="noreferrer noopener nofollow">[email protected]</a>")
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())
messages.add_message(request, messages.SUCCESS, str(payment.id) + response.status_code + response.body + response.headers)
并出现错误
HTTP Error 401: Unauthorized
可能是什么问题?
> Traceback Traceback: File
> "C:\Users\PAPA\DEV\rent_unit\rent_unit_venv\lib\site-packages\django\core\handlers\base.py"
> in get_response
> 132. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\PAPA\DEV\rent_unit\rent_unit_venv\lib\site-packages\django\contrib\auth\decorators.py"
> in _wrapped_view
> 22. return view_func(request, *args, **kwargs) File "C:\Users\PAPA\DEV\rent_unit\src\payment\views.py" in payment_new
> 251. response = sg.client.mail.send.post(request_body=mail.get()) File
> "C:\Users\PAPA\DEV\rent_unit\rent_unit_venv\lib\site-packages\python_http_client\client.py"
> in http_request
> 204. return Response(self._make_request(opener, request)) File
> "C:\Users\PAPA\DEV\rent_unit\rent_unit_venv\lib\site-packages\python_http_client\client.py"
> in _make_request
> 138. return opener.open(request) File "c:\python27\Lib\urllib2.py" in open
> 435. response = meth(req, response) File "c:\python27\Lib\urllib2.py" in http_response
> 548. 'http', request, response, code, msg, hdrs) File "c:\python27\Lib\urllib2.py" in error
> 473. return self._call_chain(*args) File "c:\python27\Lib\urllib2.py" in _call_chain
> 407. result = func(*args) File "c:\python27\Lib\urllib2.py" in http_error_default
> 556. raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
>
> Exception Type: HTTPError at
> /payment/new/28/http://127.0.0.1:8000/lease/payment_details/28/
> Exception Value: HTTP Error 401: Unauthorized
最佳答案
问题正如它所说的那样;你没有被授权。您很可能未设置 API key 。
您添加到问题中的说明显示 SENDGRID_API_KEY
已添加到 Django settings.py
中,而您的代码显示您从环境变量中获取数据。
环境变量方法
如果您使用环境变量方法,请确保您已设置一个名为 SENDGRID_API_KEY
的环境变量。您可以通过打开 python 控制台并输入以下内容来检查它是否已设置:
import os
os.environ.get('SENDGRID_API_KEY')
如果 key 没有打印出来,则意味着它丢失了。每个操作系统都有不同的 setting environment variables permanently 方式,所以我不会在这里将它们全部列出。
Settings.py 方法
如果您要使用 Django settings.py
方法,只需替换:
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
与
from django.conf import settings
sg = sendgrid.SendGridAPIClient(apikey=settings.SENDGRID_API_KEY)
Settings.py 方法中的环境变量
最后,由于settings.py
是一个可执行的python文件,您还可以在那里执行环境变量导入。这样做的好处是可以从系统级别或 Heroku 控制台进行调整,但仍然使用 settings.py
。
# inside settings.py
import os
SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY')
关于python - 在使用 sendgrid 设置 django 期间出现错误 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44233399/