From: Menno Smits Date: Tue, 27 Oct 2015 05:22:12 +0000 (+1300) Subject: Added livetest support for OAUTH2 with Yahoo X-Git-Tag: 1.0.0~1^2~11 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=cd00f033d21738779b165852c2afb9584569da7d;p=imapclient.git Added livetest support for OAUTH2 with Yahoo --- diff --git a/imapclient/config.py b/imapclient/config.py index cc80213..1b19ddd 100644 --- a/imapclient/config.py +++ b/imapclient/config.py @@ -131,32 +131,39 @@ def _read_config_section(parser, section): expect_failure=get('expect_failure') ) +OAUTH2_REFRESH_URLS = { + 'imap.gmail.com': 'https://accounts.google.com/o/oauth2/token', + 'imap.mail.yahoo.com': "https://api.login.yahoo.com/oauth2/get_token", +} -def refresh_oauth2_token(client_id, client_secret, refresh_token): +def refresh_oauth2_token(hostname, client_id, client_secret, refresh_token): if not json: - raise RuntimeError("livetest OAUTH2 functionality relies on 'json' module") + raise RuntimeError("OAUTH2 functionality relies on 'json' module") + + url = OAUTH2_REFRESH_URLS.get(hostname) + if not url: + raise ValueError("don't know where to refresh OAUTH2 token for %r" % hostname) + 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() + response = urlopen(url, 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 = {} - -def get_oauth2_token(client_id, client_secret, refresh_token): - cache_key = (client_id, client_secret, refresh_token) +def get_oauth2_token(hostname, client_id, client_secret, refresh_token): + cache_key = (hostname, client_id, client_secret, refresh_token) token = _oauth2_cache.get(cache_key) if token: return token - token = refresh_oauth2_token(client_id, client_secret, refresh_token) + + token = refresh_oauth2_token(hostname, client_id, client_secret, refresh_token) _oauth2_cache[cache_key] = token return token - def create_client_from_config(conf): ssl_context = None if conf.ssl: @@ -181,7 +188,8 @@ def create_client_from_config(conf): conf.oauth_token, conf.oauth_token_secret) elif conf.oauth2: - access_token = get_oauth2_token(conf.oauth2_client_id, + access_token = get_oauth2_token(conf.host, + conf.oauth2_client_id, conf.oauth2_client_secret, conf.oauth2_refresh_token) client.oauth2_login(conf.username, access_token)