From bafdee0cd7adacdecefbfa62d773c85e07650f12 Mon Sep 17 00:00:00 2001 From: Michael Krug Date: Fri, 14 Jul 2017 13:24:53 +0200 Subject: [PATCH 1/2] improve headers, add locale --- pgoapi/auth_ptc.py | 29 +++++++++++++++++++++-------- pgoapi/pgoapi.py | 17 +++++++++++------ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/pgoapi/auth_ptc.py b/pgoapi/auth_ptc.py index 8b6909f7..611e9e3b 100755 --- a/pgoapi/auth_ptc.py +++ b/pgoapi/auth_ptc.py @@ -40,21 +40,33 @@ class AuthPtc(Auth): - PTC_LOGIN_URL1 = 'https://sso.pokemon.com/sso/oauth2.0/authorize?client_id=mobile-app_pokemon-go&redirect_uri=https%3A%2F%2Fwww.nianticlabs.com%2Fpokemongo%2Ferror' - PTC_LOGIN_URL2 = 'https://sso.pokemon.com/sso/login?service=http%3A%2F%2Fsso.pokemon.com%2Fsso%2Foauth2.0%2FcallbackAuthorize' + PTC_LOGIN_URL1 = 'https://sso.pokemon.com/sso/oauth2.0/authorize' + PTC_LOGIN_URL2 = 'https://sso.pokemon.com/sso/login' PTC_LOGIN_OAUTH = 'https://sso.pokemon.com/sso/oauth2.0/accessToken' PTC_LOGIN_CLIENT_SECRET = 'w8ScCUXJQc6kXKw8FiOhd8Fixzht18Dq3PEVkUCP5ZPxtgyWsbTvWHFLm2wNY0JR' - def __init__(self, username=None, password=None, user_agent=None, timeout=None): + def __init__(self, username=None, password=None, user_agent=None, timeout=None, locale=None): Auth.__init__(self) self._auth_provider = 'ptc' - self._session = requests.session() - self._session.headers = {'User-Agent': user_agent or 'pokemongo/1 CFNetwork/811.4.18 Darwin/16.5.0', 'Host': 'sso.pokemon.com', 'X-Unity-Version': '5.5.1f1'} self._username = username self._password = password - self.timeout = timeout or 15 + + self.locale = locale or 'en_US' + self.timeout = timeout or 10 + + self._session = requests.session() + + self._session.headers = { + 'Accept': '*/*', + 'Host': 'sso.pokemon.com', + 'Connection': 'keep-alive', + 'User-Agent': user_agent or 'pokemongo/1 CFNetwork/811.4.18 Darwin/16.5.0', + 'Accept-Language': self.locale.lower().replace('_', '-'), + 'Accept-Encoding': 'gzip-deflate', + 'X-Unity-Version': '5.5.1f1' + } def set_proxy(self, proxy_config): self._session.proxies = proxy_config @@ -70,7 +82,7 @@ def user_login(self, username=None, password=None, retry=True): now = get_time() try: - r = self._session.get(self.PTC_LOGIN_URL1, timeout=self.timeout) + r = self._session.get(self.PTC_LOGIN_URL1, params={'client_id': 'mobile-app_pokemon-go', 'redirect_uri': 'https://www.nianticlabs.com/pokemongo/error', 'locale': self.locale}, timeout=self.timeout) except Timeout: raise AuthTimeoutException('Auth GET timed out.') except RequestException as e: @@ -82,13 +94,14 @@ def user_login(self, username=None, password=None, retry=True): '_eventId': 'submit', 'username': self._username, 'password': self._password, + 'locale': self.locale }) except (ValueError, AttributeError) as e: self.log.error('PTC User Login Error - invalid JSON response: {}'.format(e)) raise AuthException('Invalid JSON response: {}'.format(e)) try: - r = self._session.post(self.PTC_LOGIN_URL2, data=data, timeout=self.timeout, allow_redirects=False) + r = self._session.post(self.PTC_LOGIN_URL2, params={'service': 'http://sso.pokemon.com/sso/oauth2.0/callbackAuthorize'}, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data=data, timeout=self.timeout, allow_redirects=False) except Timeout: raise AuthTimeoutException('Auth POST timed out.') except RequestException as e: diff --git a/pgoapi/pgoapi.py b/pgoapi/pgoapi.py index f97be8e7..444983a7 100755 --- a/pgoapi/pgoapi.py +++ b/pgoapi/pgoapi.py @@ -62,7 +62,12 @@ def __init__(self, provider=None, oauth2_refresh_token=None, username=None, pass self._hash_server_token = None self._session = requests.session() - self._session.headers.update({'User-Agent': 'Niantic App'}) + self._session.headers.update({ + 'User-Agent': 'Niantic App', + 'Accept': '*/*', + 'Content-Type': 'application/x-www-form-urlencoded', + 'Accept-Language': 'en-us' + }) self._session.verify = True if proxy_config is not None: @@ -77,9 +82,9 @@ def set_logger(self, logger=None): def get_api_version(): return 6702 - def set_authentication(self, provider=None, oauth2_refresh_token=None, username=None, password=None, proxy_config=None, user_agent=None, timeout=None): + def set_authentication(self, provider=None, oauth2_refresh_token=None, username=None, password=None, proxy_config=None, user_agent=None, timeout=None, locale=None): if provider == 'ptc': - self._auth_provider = AuthPtc(user_agent=user_agent, timeout=timeout) + self._auth_provider = AuthPtc(user_agent=user_agent, timeout=timeout, locale=locale) elif provider == 'google': self._auth_provider = AuthGoogle() elif provider is None: @@ -153,9 +158,9 @@ def app_simulation_login(self): # Send empty initial request request = self.create_request() response = request.call() - + time.sleep(1.5) - + # Send GET_PLAYER only request = self.create_request() request.get_player(player_locale = {'country': 'US', 'language': 'en', 'timezone': 'America/Chicago'}) @@ -248,7 +253,7 @@ def call(self, use_dict = True): response = None execute = True - + while execute: execute = False From 1193099062f9e65722f32ceac71d7f47d6ed7b2d Mon Sep 17 00:00:00 2001 From: Michael Krug Date: Sat, 15 Jul 2017 15:54:20 +0200 Subject: [PATCH 2/2] use variables for params and headers --- pgoapi/auth_ptc.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pgoapi/auth_ptc.py b/pgoapi/auth_ptc.py index 611e9e3b..bcc48db0 100755 --- a/pgoapi/auth_ptc.py +++ b/pgoapi/auth_ptc.py @@ -81,8 +81,14 @@ def user_login(self, username=None, password=None, retry=True): self._session.cookies.clear() now = get_time() + get_params = { + 'client_id': 'mobile-app_pokemon-go', + 'redirect_uri': 'https://www.nianticlabs.com/pokemongo/error', + 'locale': self.locale + } + try: - r = self._session.get(self.PTC_LOGIN_URL1, params={'client_id': 'mobile-app_pokemon-go', 'redirect_uri': 'https://www.nianticlabs.com/pokemongo/error', 'locale': self.locale}, timeout=self.timeout) + r = self._session.get(self.PTC_LOGIN_URL1, params=get_params, timeout=self.timeout) except Timeout: raise AuthTimeoutException('Auth GET timed out.') except RequestException as e: @@ -100,8 +106,16 @@ def user_login(self, username=None, password=None, retry=True): self.log.error('PTC User Login Error - invalid JSON response: {}'.format(e)) raise AuthException('Invalid JSON response: {}'.format(e)) + post_params = { + 'service': 'http://sso.pokemon.com/sso/oauth2.0/callbackAuthorize' + } + + post_headers = { + 'Content-Type': 'application/x-www-form-urlencoded' + } + try: - r = self._session.post(self.PTC_LOGIN_URL2, params={'service': 'http://sso.pokemon.com/sso/oauth2.0/callbackAuthorize'}, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data=data, timeout=self.timeout, allow_redirects=False) + r = self._session.post(self.PTC_LOGIN_URL2, params=post_params, headers=post_headers, data=data, timeout=self.timeout, allow_redirects=False) except Timeout: raise AuthTimeoutException('Auth POST timed out.') except RequestException as e: