Sendgrid-python: UnauthorizedError: HTTP Error 401: Unauthorized error appears when sending e-mail

Created on 2 Mar 2018  路  16Comments  路  Source: sendgrid/sendgrid-python

Issue Summary

I can send e-mails with my api key by using curl with following examples: https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/curl_examples.html

But when I try to send an e-mail with "Hello Mail/With Mail Helper Class" code, I get following error:

Traceback (most recent call last):
  File "gridsend.py", line 11, in <module>
    response = sg.client.mail.send.post(request_body=mail.get())
  File "/usr/local/lib/python3.6/site-packages/python_http_client/client.py", line 227, in http_request
    return Response(self._make_request(opener, request))
  File "/usr/local/lib/python3.6/site-packages/python_http_client/client.py", line 161, in _make_request
    raise exc
python_http_client.exceptions.UnauthorizedError: HTTP Error 401: Unauthorized

Technical details:

  • sendgrid-python Version: 5.3.0
  • Python Version: 3.6
easy help wanted help wanted docs update up for grabs up-for-grabs

Most helpful comment

Now I understand the issue. Example code contains this code apikey=os.environ.get('SENDGRID_API_KEY') what I understand from here, I just need to type my API key to here. However, it's the name of the environment variable. Documentation is not clear on this and it's very confusing. I suggest you to make this point clear for everyone. Cheers

All 16 comments

Now I understand the issue. Example code contains this code apikey=os.environ.get('SENDGRID_API_KEY') what I understand from here, I just need to type my API key to here. However, it's the name of the environment variable. Documentation is not clear on this and it's very confusing. I suggest you to make this point clear for everyone. Cheers

I have reopened this issue so that we can update the README to make this issue clearer.

Thanks @utkusen!

In addition to making that line clear in the python code, I think the README should also be updated with how to properly set the environment variable for the api key on windows. I found(after multiple attempts) that typing set SENDGRID_API_KEY=<insert_your_api_key> into the cli works. @thinkingserious

Thank you @bchandaka!

If you don't mind and have the time, we are accepting PRs :)

Hello, i have the same issue. I set the environment variable in the command prompt (with and without quote, i was not sure) but the example code gave me 401 error. I have an api key and api key pw, do i need the pw somewhere ? thanks

My code worked with just the api key and I don't think I even got an api key password. Did you try printing the api key variable in the cli or in your python program to see if the variable is being read/set properly?
@tollvam

@bchandaka Yes i checked and the class variable self.apikey is well set with my api key

Hi @tollvam,

To confirm it's not an API Key issue, please try making the call here with your API Key.

Thanks!

With Best Regards,

Elmer

Hi @thinkingserious, thanks for the link. It says "The provided authorization grant is invalid, expired, or revoked" so i have to regenerate one ? thanks

Hello @tollvam,

Yes, that is what I would try.

Thanks!

With Best Regards,

Elmer

Just to be sure, do i have to put something in "Query String" on the page to test ?

Here is an even easier method to test your API Key.

OMG, I just quickly copy-pasted this example to test if my set-up works, then I spent 40 minutes trying to understand what's wrong with my account .... :(
gotta call it today

therer is answer but i want to add more info. I think that this is because window os.
image

refer this https://github.com/sendgrid/sendgrid-python

and
set SENDGRID_API_KEY=SG.FHWXmV68Td2cEYJQrPjDdQ.I1VEkE2CBg7--r7QfS-AzhfSU5 ( !!! do not use ' ' like : 'SG.FHWXmV68Td2cEYJQrPjDdQ.I1VEkE2CBg7--r7QfS-AzhfSU5' ).
also. Do not replace SENDGRID_API_KEY line with your API_KEY : sg =SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) .

Hi, I am still facing a 401 error from my python code. I have checked my environment variable is set properly as well.
What should I do ?? :(

@MuhammadSufyanMalik Try logging the error response body to get more details about why it's failing: print(e.body)

Was this page helpful?
0 / 5 - 0 ratings