Sendgrid-python: HTTP Error 401:Unauthorized with Cronjob

Created on 4 Sep 2019  路  6Comments  路  Source: sendgrid/sendgrid-python

Hey guys! I am getting the HTTP Error 401. It works fine when I directly run the code. But I am trying to automate it using cronjob on Ubuntu and it keeps giving me this error. I have also tested my API key by sending a request to "https://sendgrid.com/docs/for-developers/sending-email/curl-examples/#-Hello-World".

Any help would be appreciated!

Traceback (most recent call last):
File "/home/akshay/.local/lib/python3.6/site-packages/python_http_client/client.py", line 172, in _make_request
return opener.open(request, timeout=timeout)
File "/usr/lib/python3.6/urllib/request.py", line 532, in open
response = meth(req, response)
File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.6/urllib/request.py", line 570, in error
return self._call_chain(*args)
File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/usr/lib/python3.6/urllib/request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "./apicall.py", line 46, in 
response = sg.send(message)
File "/home/akshay/.local/lib/python3.6/site-packages/sendgrid/sendgrid.py", line 98, in send
response = self.client.mail.send.post(request_body=message.get())
File "/home/akshay/.local/lib/python3.6/site-packages/python_http_client/client.py", line 252, in http_request
return Response(self._make_request(opener, request, timeout=timeout))
File "/home/akshay/.local/lib/python3.6/site-packages/python_http_client/client.py", line 174, in _make_request
exc = handle_error(err)
File "/home/akshay/.local/lib/python3.6/site-packages/python_http_client/exceptions.py", line 82, in handle_error
exc = err_dicterror.code
File "/home/akshay/.local/lib/python3.6/site-packages/python_http_client/exceptions.py", line 10, in init
self.body = error.read()
File "/usr/lib/python3.6/tempfile.py", line 624, in func_wrapper
return func(*args, **kwargs)
File "/usr/lib/python3.6/http/client.py", line 462, in read
s = self._safe_read(self.length)
File "/usr/lib/python3.6/http/client.py", line 614, in _safe_read
raise IncompleteRead(b''.join(s), amt)
http.client.IncompleteRead: IncompleteRead(0 bytes read, 116 more expected)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "./apicall.py", line 49, in 
print(e.message)
AttributeError: 'IncompleteRead' object has no attribute 'message'
easy work in progress question

All 6 comments

I looked at your repo, this doesn't look like an issue with sendgrid, more your code.

_SendGridAPIClient(os.environ.get('$SENDGRID_API_KEY'))_

you're getting your apikey from your environment, your cron user probably doesn't have the same as your normal user.

@kalkaran thanks for your comment. I did check the cron user, it has the same environment variable. Do you think adding the API Key instead of the environment variable will work?

Have you considered setting up a txt file and having the variables you pass (api key, message,...) output into it, and having it triggered by cron? That should show what you don't have access to.

Personally I got round this issue but setting up a screen environment and leaving my python script running in it.

Hi @asing012,

It looks like you might be grabbing your SENDGRID_API_KEY environment variable incorrectly. Note that the correct way to initialize your SendGridAPIClient object is:

sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))

It's a subtle difference, but your code was looking for an environment variable called $SENDGRID_API_KEY instead of SENDGRID_API_KEY.

@eshanholtz thank you for the comment. It is working now.

If you have this problem but your API is valid, it might be that your IP is not correctly whitelisted

Was this page helpful?
0 / 5 - 0 ratings