sendgrid / sendgrid-python

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

UnauthorizedError: HTTP Error 401: Unauthorized error appears when sending e-mail #887

Closed MuhammadSufyanMalik closed 4 years ago

MuhammadSufyanMalik commented 4 years ago

Issue Summary

I am facing this issue still, I have checked all precautions which are giving old issues, my key is set properly. what can i do ?

Code Snippet

import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
    from_email='myemail@gmail.com',
    to_emails='user'semail@gmail.com',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')
try:
    sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sg.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e)

Exception/Log

HTTP Error 401: Unauthorized

Technical details:

childish-sambino commented 4 years ago

As stated here, try logging the error response body to get more details about why it's failing: print(e.body)

MuhammadSufyanMalik commented 4 years ago

Thanks,