Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
utkusen opened this issue Mar 2, 2018 · 16 comments
Labels
difficulty: easy fix is easy in difficulty status: help wanted requesting help from the community type: docs update documentation change not affecting the code

Comments

@utkusen
Copy link

utkusen commented Mar 2, 2018

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
@utkusen
Copy link
Author

utkusen commented Mar 2, 2018

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

@thinkingserious
Copy link
Contributor

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

Thanks @utkusen!

@thinkingserious thinkingserious added status: help wanted requesting help from the community difficulty: easy fix is easy in difficulty up-for-grabs type: docs update documentation change not affecting the code labels Mar 5, 2018
@bchandaka
Copy link
Contributor

bchandaka commented Jun 28, 2018

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

@thinkingserious
Copy link
Contributor

Thank you @bchandaka!

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

@bchandaka bchandaka mentioned this issue Jul 9, 2018
@tollvam
Copy link

tollvam commented Aug 3, 2018

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

@bchandaka
Copy link
Contributor

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

@tollvam
Copy link

tollvam commented Aug 3, 2018

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

@thinkingserious
Copy link
Contributor

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

@tollvam
Copy link

tollvam commented Aug 6, 2018

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

@thinkingserious
Copy link
Contributor

Hello @tollvam,

Yes, that is what I would try.

Thanks!

With Best Regards,

Elmer

@tollvam
Copy link

tollvam commented Aug 8, 2018

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

@thinkingserious
Copy link
Contributor

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

@sr9yar
Copy link

sr9yar commented Dec 20, 2018

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

@JoeKhoa
Copy link

JoeKhoa commented Apr 7, 2020

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')) .

@MuhammadSufyanMalik
Copy link

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 ?? :(

@childish-sambino
Copy link
Contributor

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

@sendgrid sendgrid locked as resolved and limited conversation to collaborators May 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
difficulty: easy fix is easy in difficulty status: help wanted requesting help from the community type: docs update documentation change not affecting the code
Projects
None yet
Development

No branches or pull requests

8 participants