From 9e700f99b6771408abd11e0576c42a2806cd9539 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 6 Jan 2018 13:53:56 -0800 Subject: [PATCH] Extract access_token from response URL --- wideq.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/wideq.py b/wideq.py index 7217b4d..9adbbd9 100644 --- a/wideq.py +++ b/wideq.py @@ -1,5 +1,5 @@ import requests -from urllib.parse import urljoin, urlencode +from urllib.parse import urljoin, urlencode, urlparse, parse_qs GATEWAY_URL = 'https://kic.lgthinq.com:46030/api/common/gatewayUriList' @@ -14,6 +14,9 @@ CLIENT_ID = 'LGAO221A02' def gateway_info(): + """Load information about the hosts to use for API interaction. + """ + req_data = {DATA_ROOT: {'countryCode': COUNTRY, 'langCode': LANGUAGE}} headers = { 'x-thinq-application-key': APP_KEY, @@ -25,6 +28,10 @@ def gateway_info(): def oauth_url(oauth_base): + """Construct the URL for users to log in (in a browser) to start an + authenticated session. + """ + url = urljoin(oauth_base, OAUTH_PATH) query = urlencode({ 'country': COUNTRY, @@ -38,8 +45,20 @@ def oauth_url(oauth_base): return '{}?{}'.format(url, query) +def parse_oauth_callback(url): + """Parse the URL to which an OAuth login redirected to obtain an + access token for API credentials. + """ + + params = parse_qs(urlparse(url).query) + return params['access_token'][0] + + if __name__ == '__main__': - gw = gateway_info() - oauth_base = gw['empUri'] - api_root = gw['thinqUri'] - print(oauth_url(oauth_base)) + # gw = gateway_info() + # oauth_base = gw['empUri'] + # api_root = gw['thinqUri'] + # print(oauth_url(oauth_base)) + + access_token = parse_oauth_callback(input()) + print(access_token)