我已将sendgrid添加到我的django应用程序中 按照此处的简单步骤https://github.com/elbuo8/sendgrid-django
生成了帐户,并在sengrid网站上复制了api
在我的视图中添加了代码
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("commerce@gmail.com")
to_email = Email("lopa@gmail.com")
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错误401:未经授权
可能是什么问题?
> 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
答案 0 :(得分:2)
问题正是它所说的;您未获得授权。很可能你没有设置API密钥。
您添加到问题中的说明会显示SENDGRID_API_KEY
被添加到Django settings.py
,而您的代码则显示您是从环境变量中提取的。
如果您正在使用环境变量方法,请确保您已设置名为SENDGRID_API_KEY
的环境变量。您可以通过打开python控制台并键入:
import os
os.environ.get('SENDGRID_API_KEY')
如果没有打印出密钥,则表示该密钥丢失。每个操作系统都有setting environment variables permanently的不同方式,所以我不打算将它们全部列在这里。
如果您使用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
是一个可执行的python文件,您还可以在那里执行环境变量导入。这样做的好处是可以从系统级别或Heroku控制台进行调整,但仍然使用settings.py
。
# inside settings.py
import os
SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY')
答案 1 :(得分:0)
如果您不想使用环境变量或设置,只需更新代码以使用所需的代码即可
SENDGRID_API_KEY =' * sendgrid *** api * '
sg = sendgrid.SendGridAPIClient(apikey = SENDGRID_API_KEY)
答案 2 :(得分:0)
我也遇到了同样的错误,我使用此cli来解决:例如(my_api_key) 设置SENDGRID_API_KEY = SG.FHWXmV68Td2cEYJQrPjDdQ.I1VEkE2CBg7--r7QfS-AzhfSU5 (!!!不要使用'',例如:'SG.FHWXmV68Td2cEYJQrPjDdQ.I1VEkE2CBg7--r7QfS-AzhfSU5')。 也。 请勿将SENDGRID_API_KEY行替换为您的API_KEY: sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) 。您应该参考此文档: https://github.com/sendgrid/sendgrid-python 我认为这个问题是因为窗口用户看到此,您将理解。访问此文档: