]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Fixed livetest/interact OAUTH handling under Python 3
authorMenno Smits <menno@freshfoo.com>
Mon, 12 Jan 2015 10:59:37 +0000 (23:59 +1300)
committerMenno Smits <menno@freshfoo.com>
Mon, 12 Jan 2015 10:59:37 +0000 (23:59 +1300)
Fixes #130.

imapclient/config.py

index b3eb3b97a6429a2938a8d6bb18b178d3ed6273de..3ddd10f4ba30a3b93232d082d98cbdd8b6c4c039 100644 (file)
@@ -6,7 +6,8 @@ except ImportError:
     from configparser import SafeConfigParser, NoOptionError
 
 import imapclient
-import urllib
+from .six.moves.urllib.request import urlopen
+from .six.moves.urllib.parse import urlencode
 
 try:
     import json
@@ -66,13 +67,13 @@ def parse_config_file(path):
 def refresh_oauth2_token(client_id, client_secret, refresh_token):
     if not json:
         raise RuntimeError("livetest OAUTH2 functionality relies on 'json' module")
-    post = dict(client_id=client_id,
-                client_secret=client_secret,
-                refresh_token=refresh_token,
-                grant_type='refresh_token')
-    response = urllib.urlopen('https://accounts.google.com/o/oauth2/token',
-                              urllib.urlencode(post)).read()
-    return json.loads(response)['access_token']
+    post = dict(client_id=client_id.encode('ascii'),
+                client_secret=client_secret.encode('ascii'),
+                refresh_token=refresh_token.encode('ascii'),
+                grant_type=b'refresh_token')
+    response = urlopen('https://accounts.google.com/o/oauth2/token',
+                       urlencode(post).encode('ascii')).read()
+    return json.loads(response.decode('ascii'))['access_token']
 
 # Tokens are expensive to refresh so use the same one for the duration of the process.
 _oauth2_cache = {}