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

oauth2.py stopped to work - urllib.py, httplib.py and socket.py issues #31

Closed
marcelolaia opened this issue Aug 17, 2020 · 8 comments
Closed

Comments

@marcelolaia
Copy link

marcelolaia commented Aug 17, 2020

Hi,

I use this approach to send my emails by msmtp. However, oauth2.py stopped to work 08-15-2020.

Traceback (most recent call last):
  File "/home/myuser/bin/OAuth2/oauth2.py", line 347, in <module>
    main(sys.argv)
  File "/home/myuser/bin/OAuth2/oauth2.py", line 307, in main
    options.refresh_token)
  File "/home/myuser/bin/OAuth2/oauth2.py", line 238, in RefreshToken
    response = urllib.urlopen(request_url, urllib.urlencode(params)).read()
  File "/usr/lib/python2.7/urllib.py", line 89, in urlopen
    return opener.open(url, data)
  File "/usr/lib/python2.7/urllib.py", line 217, in open
    return getattr(self, name)(url, data)
  File "/usr/lib/python2.7/urllib.py", line 445, in open_https
    h.endheaders(data)
  File "/usr/lib/python2.7/httplib.py", line 1078, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 894, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 856, in send
    self.connect()
  File "/usr/lib/python2.7/httplib.py", line 1295, in connect
    HTTPConnection.connect(self)
  File "/usr/lib/python2.7/httplib.py", line 833, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python2.7/socket.py", line 557, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno -2] Name or service not known

I made a test and run:

$ ./oauth2.py --user=myuser@domain.edu.br --client_id=9876........apps.googleusercontent.com --client_secret=***************************************** --generate_oauth2_token
To authorize token, visit this url and follow the directions:
  https://accounts.google.com/o/oauth2/auth?client_id=4485...........apps.googleusercontent.com&redirect_uri=*********************************mail.google.com%2F

I followed that link and made authorization. So, I got the verification code and enter it on the prompt Enter verification code:

After that, I got the same error messages as above:

Traceback (most recent call last):
  File "./oauth2.py", line 366, in <module>
    main(sys.argv)
  File "./oauth2.py", line 345, in main
    authorization_code)
  File "./oauth2.py", line 228, in AuthorizeTokens
    response = urlopen(request_url, data).read()
  File "/usr/lib/python2.7/urllib.py", line 89, in urlopen
    return opener.open(url, data)
  File "/usr/lib/python2.7/urllib.py", line 217, in open
    return getattr(self, name)(url, data)
  File "/usr/lib/python2.7/urllib.py", line 445, in open_https
    h.endheaders(data)
  File "/usr/lib/python2.7/httplib.py", line 1078, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 894, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 856, in send
    self.connect()
  File "/usr/lib/python2.7/httplib.py", line 1295, in connect
    HTTPConnection.connect(self)
  File "/usr/lib/python2.7/httplib.py", line 833, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python2.7/socket.py", line 557, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno -2] Name or service not known
myuser@myuser:~/bin/OAuth2$ 

I have applied the patch for python 3.0 and 2.7 compatibility (here).

Any thoughts?

Thank you

@marcelolaia marcelolaia changed the title oauth2.py possible blocked by VPN or Firewall oauth2.py stopped to work - urllib.py, httplib.py and socket.py issues Aug 18, 2020
@marcelolaia
Copy link
Author

offlineimap work very nice without any issues.

@shiben-lab
Copy link

@marcelolaia Could you please share the offlineimap change details ? I am also stuck in the above issue you have mentioned here.

@marcelolaia
Copy link
Author

@marcelolaia Could you please share the offlineimap change details ? I am also stuck in the above issue you have mentioned here.

Hi @shiben-lab, I'm so sorry for delay in replay.
I have been set up my .offlineimaprc like the instructions here.
The relevant part is:

[Repository ExampleCompanyRemote]
type = IMAP
remotehost = imap.gmail.com
remoteuser = myuser
ssl = yes
starttls = no
sslcacertfile = /etc/ssl/certs/ca-certificates.crt
auth_mechanisms = XOAUTH2
oauth2_client_id = YOUR_CLIENT_ID_HERE
oauth2_client_secret = YOUR_CLIENT_SECRET_HERE
oauth2_request_url = https://accounts.google.com/o/oauth2/token
oauth2_refresh_token = YOUR_REFRESH_TOKEN_HERE

Please, tell me if you have solved your issue.

My msmtp issue continue unsolved.

@marcelolaia
Copy link
Author

I tried it with python 3.8 and it not run:

 Traceback (most recent call last):
  File "/usr/lib/python3.8/urllib/request.py", line 1350, in do_open h.request(req.get_method(), req.selector, req.data, headers,
  File "/usr/lib/python3.8/http/client.py", line 1255, in request self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1301, in _send_request self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1250, in endheaders self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1010, in _send_output self.send(msg)
  File "/usr/lib/python3.8/http/client.py", line 950, in send self.connect()
  File "/usr/lib/python3.8/http/client.py", line 1417, in connect super().connect()
  File "/usr/lib/python3.8/http/client.py", line 921, in connect self.sock = self._create_connection(
  File "/usr/lib/python3.8/socket.py", line 787, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./oauth2.py", line 366, in <module> main(sys.argv)
  File "./oauth2.py", line 344, in main response = AuthorizeTokens(options.client_id, options.client_secret,
  File "./oauth2.py", line 228, in AuthorizeTokens response = urlopen(request_url, data).read()
  File "/usr/lib/python3.8/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout)
  File "/usr/lib/python3.8/urllib/request.py", line 525, in open response = self._open(req, data)
  File "/usr/lib/python3.8/urllib/request.py", line 542, in _open result = self._call_chain(self.handle_open, protocol, protocol +
  File "/usr/lib/python3.8/urllib/request.py", line 502, in _call_chain result = func(*args)
  File "/usr/lib/python3.8/urllib/request.py", line 1393, in https_open return self.do_open(http.client.HTTPSConnection, req,
  File "/usr/lib/python3.8/urllib/request.py", line 1353, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>

@MunifTanjim
Copy link

Hey @marcelolaia,

If you are still stuck, you can try this minimal script I wrote for myself: gmail-oauth2_none_none.sh

It just requires bash, curl and jq on your system.

@marcelolaia
Copy link
Author

Hi @MunifTanjim, thank you so much! Please, could you share the needed configurations to do? You use it with msmtp?

@MunifTanjim
Copy link

MunifTanjim commented Oct 19, 2020

You use it with msmtp?

Yes.

You can put this in your msmtp config:

auth oauthbearer
passwordeval "gmail-oauth2.sh access_token --client-id CLIENT_ID --client-secret CLIENT_SECRET --refresh_token REFRESH_TOKEN"

You can get the REFRESH_TOKEN by running this:

gmail-oauth2.sh refresh_token --client-id CLIENT_ID --client-secret CLIENT_SECRET

If you don't want to put these sensitive CLIENT_SECRET or REFRESH_TOKEN in your config file you can take a look at another script that I actually use: gmail-oauth2_bitwarden_secret-tool.sh

It needs Bitwarden CLI bw and secret-tool. With it, I just put these in my configs:

# msmtp
auth oauthbearer
passwordeval "gmail-oauth2.sh access_token me@example.com"

# mbsync
AuthMechs XOAUTH2
PassCmd "gmail-oauth2.sh access_token me@exmaple.com"

@junyer
Copy link
Collaborator

junyer commented Jul 24, 2023

Fixed in commit 944cffd.

@junyer junyer closed this as completed Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants