Sendgrid与Cloud9通过WebApi Python选项
问题描述:
我在使用我的Cloud9 Python环境中的sendgrid时遇到问题。这是SendGrid设置选项建议的代码。Sendgrid与Cloud9通过WebApi Python选项
注意:我生成了一个实际的API,显然“YOUR_API_KEY”已被适当的键替换。
回声 “出口SENDGRID_API_KEY = 'YOUR_API_KEY'”> sendgrid.env
回声 “sendgrid.env” >>的.gitignore
源./sendgrid.env**
PIP安装sendgrid
# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
import sendgrid
import os
from sendgrid.helpers.mail import *
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("[email protected]")
to_email = Email("[email protected]")
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())
print(response.status_code)
print(response.body)
print(response.headers)
收到此错误日志
> Traceback (most recent call last): File "schedule.py", line 92, in
> <module>
> response = sg.client.mail.send.post(request_body=mail.get()) File
> "/usr/local/lib/python2.7/dist-packages/python_http_client/client.py",
> line 204, in http_request
> return Response(self._make_request(opener, request)) File "/usr/local/lib/python2.7/dist-packages/python_http_client/client.py",
> line 138, in _make_request
> return opener.open(request) File "/usr/lib/python2.7/urllib2.py", line 410, in open
> response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
> 'http', request, response, code, msg, hdrs) File "/usr/lib/python2.7/urllib2.py", line 448, in error
> return self._call_chain(*args) File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
> result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
> raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 401: Unauthorized
那么这与缺少库有关吗?一些Cloud9限制?它不应该,因为我甚至没有搞乱SMTP选项。,
答
所以我发现Cloud9不让你设置环境变量,所以我不得不在上面的代码中对我的API KEY进行硬编码。
所有功劳归于作者Using Cloud9 IDE and Mailgun getting this error Net::SMTPSyntaxError in Devise::RegistrationsController#create,在处理类似问题时发现此问题。
谢谢!