Sendgrid-python: IncompleteRead: IncompleteRead(0 bytes read, 116 more expected) -- HTTPError: HTTP Error 401: Unauthorized

Created on 16 May 2018  路  4Comments  路  Source: sendgrid/sendgrid-python

Issue Summary

It's my first time using sendgrid with python. When I try to send email I got that issue

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/python_http_client/client.py in _make_request(self, opener, request)
    156         try:
--> 157             return opener.open(request)
    158         except HTTPError as err:

/usr/lib/python3.5/urllib/request.py in open(self, fullurl, data, timeout)
    471             meth = getattr(processor, meth_name)
--> 472             response = meth(req, response)
    473 

/usr/lib/python3.5/urllib/request.py in http_response(self, request, response)
    581             response = self.parent.error(
--> 582                 'http', request, response, code, msg, hdrs)
    583 

/usr/lib/python3.5/urllib/request.py in error(self, proto, *args)
    509             args = (dict, 'default', 'http_error_default') + orig_args
--> 510             return self._call_chain(*args)
    511 

/usr/lib/python3.5/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    443             func = getattr(handler, meth_name)
--> 444             result = func(*args)
    445             if result is not None:

/usr/lib/python3.5/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    589     def http_error_default(self, req, fp, code, msg, hdrs):
--> 590         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    591 

HTTPError: HTTP Error 401: Unauthorized

During handling of the above exception, another exception occurred:

IncompleteRead                            Traceback (most recent call last)
<ipython-input-4-44e99a2b87b3> in <module>()
     11 mail = Mail(from_email, subject, to_email, content)
     12 
---> 13 response = sg.client.mail.send.post(request_body=mail.get())
     14 print(response.status_code)
     15 print(response.body)

/usr/local/lib/python3.5/dist-packages/python_http_client/client.py in http_request(*_, **kwargs)
    225                     request.add_header('Content-Type', 'application/json')
    226                 request.get_method = lambda: method
--> 227                 return Response(self._make_request(opener, request))
    228             return http_request
    229         else:

/usr/local/lib/python3.5/dist-packages/python_http_client/client.py in _make_request(self, opener, request)
    157             return opener.open(request)
    158         except HTTPError as err:
--> 159             exc = handle_error(err)
    160             exc.__cause__ = None
    161             raise exc

/usr/local/lib/python3.5/dist-packages/python_http_client/exceptions.py in handle_error(error)
     79 def handle_error(error):
     80     try:
---> 81         exc = err_dict[error.code](error)
     82     except KeyError:
     83         return HTTPError(error)

/usr/local/lib/python3.5/dist-packages/python_http_client/exceptions.py in __init__(self, error)
      7         self.status_code = error.code
      8         self.reason = error.reason
----> 9         self.body = error.read()
     10         self.headers = error.hdrs
     11 

/usr/lib/python3.5/tempfile.py in func_wrapper(*args, **kwargs)
    620             @_functools.wraps(func)
    621             def func_wrapper(*args, **kwargs):
--> 622                 return func(*args, **kwargs)
    623             # Avoid closing the file as long as the wrapper is alive,
    624             # see issue #18879.

/usr/lib/python3.5/http/client.py in read(self, amt)
    459             else:
    460                 try:
--> 461                     s = self._safe_read(self.length)
    462                 except IncompleteRead:
    463                     self._close_conn()

/usr/lib/python3.5/http/client.py in _safe_read(self, amt)
    607             chunk = self.fp.read(min(amt, MAXAMOUNT))
    608             if not chunk:
--> 609                 raise IncompleteRead(b''.join(s), amt)
    610             s.append(chunk)
    611             amt -= len(chunk)

IncompleteRead: IncompleteRead(0 bytes read, 116 more expected)

Here is my code:

data = {
    'name': "John"
}
email = jinja2.Environment().from_string(email).render(name=data['name']) 

from_email = Email("[email protected]")
to_email = Email("[email protected]")
subject = "{name} nos 50 coursiers sont 脿 votre disposition".format(name=data['name'])
content = Content("text/html", email)
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)

Technical details:

  • sendgrid-python Version: master (latest commit: [commit number])
  • Python Version: 3.5.2
unknown or a question

Most helpful comment

Hello @maelfosso,

A "HTTP Error 401: Unauthorized" message means there is an issue with your API Key. Can you please verify your key is valid here?

Thanks!

With Best Regards,

Elmer

All 4 comments

Hello @maelfosso,

A "HTTP Error 401: Unauthorized" message means there is an issue with your API Key. Can you please verify your key is valid here?

Thanks!

With Best Regards,

Elmer

You're right !

Thanks !!

Hello @maelfosso,

A "HTTP Error 401: Unauthorized" message means there is an issue with your API Key. Can you please verify your key is valid here?

Thanks!

With Best Regards,

Elmer

Hey,
I'm facing a similar issue as to the above user even though my API is legit and validated.Could you please help me out with it?

@Prajith3 Please open a new issue with details.

Was this page helpful?
0 / 5 - 0 ratings