I've gone through nearly everything on the help pages here but I'm still getting an HTTP Error 401: Unauthorized error.
I have created about 4 new api's and different configurations but still the same error
This is a new account and was hoping to use sendgrid for email.
I've done the curl examples and everything runs fine.
The code is directly from sendgrid:
import sendgrid
import os
from sendgrid.helpers.mail import *
SENDGRID_API_KEY = 'sendgridapi*'
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)
Hello @JohnnyQQQ,
This line: SENDGRID_API_KEY = '***sendgrid***api***' is not necessary. Your API Key should have been set in your environment like so: https://github.com/sendgrid/sendgrid-python#setup-environment-variables
The method os.environ.get gets the API key from the envoronment variable SENDGRID_API_KEY, but you have set SENDGRID_API_KEY directly. So when os.environ.get attempts to read the SENDGRID_API_KEY environment variable, it is empty, causing your API key to be blank and thus the 401 error.
With Best Regards,
Elmer
Thanks, I'm also on a windows system so I'll have to set up the path.
Just update your code to use the one you want if you prefer not to use environment variable
SENDGRID_API_KEY = '***sendgrid***api***'
sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY )
For some reason that didn't work, but once I set the environment variable it worked
I'm getting this 401 Unauthorized exception. i did this environment stuff. I'm not able to debug this exception. In local it's working, when deployed in nginx+gunicorn that time i'm getting this exception.
Hello,
I created the environment variable, with export.
And it worked.
Hello @sakthips,
I suggest that you check the value of your SendGrid API Key in your deployed environment. I'm guessing the key is not getting set properly in the deployed environment.
With Best Regards,
Elmer
i facing having same error unauthorized 401 error eve i have set enviroment variable should give sendgrid api full access from my dashboard???
resp =sg.send(message)
File "C:\Users\hpAppData\Local\Programs\Python\Python38\lib\site-packages\sendgrid\sendgrid.py", line 95, in send
response = self.client.mail.send.post(request_body=message.get())
File "C:\Users\hpAppData\Local\Programs\Python\Python38\lib\site-packages\python_http_client\client.py", line 262, in http_request
self._make_request(opener, request, timeout=timeout)
File "C:\Users\hpAppData\Local\Programs\Python\Python38\lib\site-packages\python_http_client\client.py", line 178, in _make_request
raise exc
python_http_client.exceptions.UnauthorizedError: HTTP Error 401: Unauthorized
how to fix it
@Coder0111 Please post some sample code that illustrates the issue. Are you reading the API key from an env var? If so, could you try logging the value to help debug if it's being set properly?
@childish-sambino my sendgrid details are correct though iam getting this may u help me for this

actually its all cause of this
@Coder0111 Submit a ticket here for login issues: https://support.sendgrid.com/hc/en-us/requests/new#login-issue

bro i did they relied me this as u can see in screen shot
is there another service in your mind who provide emailing api for free
There's another solution works for me to overcome HTTP Error 401: Unauthorized. You can use dotenv package instead. Here are the steps:
cd ~/my-project-direcho "export SENDGRID_API_KEY=YOUR_KEY" >> .envpip install python-dotenv in your virtualenvecho python-dotenv >> requirements.txtfrom dotenv import load_dotenv
project_folder = os.path.expanduser('~/my-project-dir') # adjust as appropriate
load_dotenv(os.path.join(project_folder, '.env'))
SECRET_KEY = os.getenv("SECRET_KEY")@arycloud bro i have been done it but still i am getting an error see in my comments i posted a screen shoot of sendgrid account they are not authorizing me to login in to my sendgrid account and when i sent them a mail what they replied me back i have post in comments see that error then let me know how to fix that error i think its all cause of that account error cause they are not letting me to login in my account
@arycloud see this bro wht they replied me https://github.com/sendgrid/sendgrid-python/issues/580#issuecomment-606175964
Most helpful comment
Just update your code to use the one you want if you prefer not to use environment variable