sendgrid / sendgrid-python

The Official Twilio SendGrid Python API Library
https://sendgrid.com
MIT License
1.55k stars 714 forks source link

HTTP Error 401:Unauthorized with Cronjob #835

Closed asing012 closed 4 years ago

asing012 commented 5 years ago

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'
kalkaran commented 5 years ago

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

_SendGridAPIClient(os.environ.get('$SENDGRID_APIKEY'))

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

asing012 commented 5 years ago

@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?

kalkaran commented 5 years ago

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.

eshanholtz commented 5 years ago

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.

asing012 commented 5 years ago

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

alichur commented 3 years ago

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