【问题标题】:During setup of django with sendgrid getting error 401在使用 sendgrid 设置 django 期间出现错误 401
【发布时间】:2017-05-29 00:58:46
【问题描述】:

我已将 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

【问题讨论】:

    标签: python django sendgrid


    【解决方案1】:

    问题正是它所说的;你没有被授权。很可能您的 API 密钥未设置。

    您添加到问题中的说明显示 SENDGRID_API_KEY 被添加到 Django settings.py,而您的代码显示您从环境变量中获取。

    环境变量方法

    如果您使用环境变量方法,请确保您已设置名为 SENDGRID_API_KEY 的环境变量。您可以通过打开 python 控制台并输入来检查它是否已设置:

    import os
    os.environ.get('SENDGRID_API_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')
    

    【讨论】:

    • 感谢@Soviut 提供最详细的答复!
    • 我也有同样的问题,但执行这些操作后,我得到了 HTTP 错误 400:错误请求错误
    【解决方案2】:

    如果您不想使用环境变量或设置,只需更新您的代码以使用您想要的代码

    SENDGRID_API_KEY = '*sendgrid***api*'

    sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)

    【讨论】:

      【解决方案3】:

      我遇到了同样的错误,我使用这个 cli 来解决:例如(my_api_key) 设置 SENDGRID_API_KEY=SG.FHWXmV68Td2cEYJQrPjDdQ.I1VEkE2CBg7--r7QfS-AzhfSU5 (!!!不要使用'',例如:'SG.FHWXmV68Td2cEYJQrPjDdQ.I1VEkE2CBg7--r7QfS-AzhfSU5')。 还。 不要用您的 API_KEY 替换 SENDGRID_API_KEY 行: sg =SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) .你应该参考这个文件: https://github.com/sendgrid/sendgrid-python 我认为这个问题是因为窗口用户,看到这个你就会明白。根据此文档:

      【讨论】:

        猜你喜欢
        • 2021-11-25
        • 2020-05-20
        • 1970-01-01
        • 1970-01-01
        • 2018-04-01
        • 1970-01-01
        • 2022-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多